This is how you fire an event:
NSNotificationCenter.defaultCenter().postNotificationName("doSomething", object: nil)
Elsewhere, listen for the event like so:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doSomething", name: "doSomething", object: nil)
Don't forget to cleanup after yourself:
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
If you want to pass data, you need to send an userInfo
object:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
NSNotificationCenter.defaultCenter().postNotificationName("doSomethingWithPayload", object: nil, userInfo: userInfo)
}
Then add a colon to your selector because you're passing a parameter:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doSomethingWithPayload:", name: "doSomething", object: nil)
Now you need to unwrap the NSNotification
object to get to the userInfo
chewy center:
func doSomethingWithPayload(notification: NSNotification){
if let payload = notification.userInfo {
}
}