swift

NSNotification

  • object is self
  • Broadcast notification:
    let center=NSNotificationCenter.defaultCenter()
    let notification=NSNotification(name: GPXURL.Notification, object: self, userInfo: [GPXURL.Key:url])
    center.postNotification(notification)
    
  • Listen:
let center=NSNotificationCenter.defaultCenter()
let queue=NSOperationQueue.mainQueue()
let appDelegate=UIApplication.sharedApplication().delegate

center.addObserverForName(GPXURL.Notification, object: appDelegate, queue: queue){ (notification) in
            if let url=notification?.userInfo?[GPXURL.Key] as? NSURL{
                self.textView.text="Recieved \(url)"
            }
        }
  • object is UITextFieldTextDidChangeNotification

    let center=NSNotificationCenter.defaultCenter()
    let queue=NSOperationQueue.mainQueue()
    center.addObserverForName(UITextFieldTextDidChangeNotification, object: nameTextField, queue: queue) { notification in
    
     }
    }