Tutorial by Examples

Naming Convention Notifications are identified by global NSString objects whose names are composed in this way: Name of associated class + Did | Will + UniquePartOfName + Notification For example: NSApplicationDidBecomeActiveNotification NSWindowDidMiniaturizeNotification NSTextViewDidChange...
Swift 2.3 //Remove observer for single notification NSNotificationCenter.defaultCenter().removeObserver(self, name: "TestNotification", object: nil) //Remove observer for all notifications NotificationCenter.defaultCenter().removeObserver(self) Swift 3 //Remove observer for s...
Swift NSNotificationCenter.defaultCenter().postNotificationName("TestNotification", object: self) Objective-C [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" object:nil];
Swift let userInfo: [String: AnyObject] = ["someKey": myObject] NSNotificationCenter.defaultCenter().postNotificationName("TestNotification", object: self, userInfo: userInfo) Objective-C NSDictionary *userInfo = [NSDictionary dictionaryWithObject:myObject forKey:@"som...
Swift func testNotification(notification: NSNotification) { let userInfo = notification.userInfo let myObject: MyObject = userInfo["someKey"] } Objective-C - (void)testNotification:(NSNotification *)notification { NSDictionary *userInfo = notification.userInfo; My...
Instead of adding an observer with a selector, a block can be used: id testObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"TestNotification" object:nil ...
// Add observer let observer = NSNotificationCenter.defaultCenter().addObserverForName("nameOfTheNotification", object: nil, queue: nil) { (notification) in // Do operations with the notification in this block } // Remove observer NSNotificationCenter.defaultCenter().removeObser...

Page 1 of 1