NSNotificationCenter
An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table.
Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.
Each running Cocoa program has a default notification center. You typically don’t create your own. An NSNotificationCenter object can deliver notifications only within a single program. If you want to post a notification to other processes or receive notifications from other processes, use an instance of NSDistributedNotificationCenter.
-
Returns the process’s default notification center.
Declaration
Swift
class func defaultCenter() -> NSNotificationCenterObjective-C
+ (NSNotificationCenter *)defaultCenterReturn Value
The current process’s default notification center, which is used for system notifications.
Availability
Available in OS X v10.0 and later.
-
Adds an entry to the receiver’s dispatch table with a notification queue and a block to add to the queue, and optional criteria: notification name and sender.
Declaration
Swift
func addObserverForName(_name: String?, objectobj: AnyObject?, queuequeue: NSOperationQueue?, usingBlockblock: (NSNotification) -> Void) -> NSObjectProtocolObjective-C
- (id<NSObject>)addObserverForName:(NSString *)nameobject:(id)objqueue:(NSOperationQueue *)queueusingBlock:(void (^)(NSNotification *note))blockParameters
nameThe name of the notification for which to register the observer; that is, only notifications with this name are used to add the block to the operation queue.
If you pass
nil, the notification center doesn’t use a notification’s name to decide whether to add the block to the operation queue.objThe object whose notifications you want to add the block to the operation queue.
If you pass
nil, the notification center doesn’t use a notification’s sender to decide whether to add the block to the operation queue.queueThe operation queue to which
blockshould be added.If you pass
nil, the block is run synchronously on the posting thread.blockThe block to be executed when the notification is received.
The block is copied by the notification center and (the copy) held until the observer registration is removed.
The block takes one argument:
notificationThe notification.
Return Value
An opaque object to act as the observer.
Discussion
If a given notification triggers more than one observer block, the blocks may all be executed concurrently with respect to one another (but on their given queue or on the current thread).
The following example shows how you can register to receive locale change notifications.
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];self.localeChangeObserver = [center addObserverForName:NSCurrentLocaleDidChangeNotification object:nilqueue:mainQueue usingBlock:^(NSNotification *note) {NSLog(@"The user's locale changed to: %@", [[NSLocale currentLocale] localeIdentifier]);}];
To unregister observations, you pass the object returned by this method to
removeObserver:. You must invokeremoveObserver:orremoveObserver:name:object:before any object specified byaddObserverForName:object:queue:usingBlock:is deallocated.NSNotificationCenter *center = [NSNotificationCenter defaultCenter];[center removeObserver:self.localeChangeObserver];
Availability
Available in OS X v10.6 and later.
-
Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.
Declaration
Swift
func addObserver(_observer: AnyObject, selectoraSelector: Selector, nameaName: String?, objectanObject: AnyObject?)Objective-C
- (void)addObserver:(id)notificationObserverselector:(SEL)notificationSelectorname:(NSString *)notificationNameobject:(id)notificationSenderParameters
notificationObserverObject registering as an observer. This value must not be
nil.notificationSelectorSelector that specifies the message the receiver sends
notificationObserverto notify it of the notification posting. The method specified bynotificationSelectormust have one and only one argument (an instance ofNSNotification).notificationNameThe name of the notification for which to register the observer; that is, only notifications with this name are delivered to the observer.
If you pass
nil, the notification center doesn’t use a notification’s name to decide whether to deliver it to the observer.notificationSenderThe object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.
If you pass
nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.Discussion
Be sure to invoke
removeObserver:name:object:beforenotificationObserveror any object specified inaddObserver:selector:name:object:is deallocated.Availability
Available in OS X v10.0 and later.
-
Removes all the entries specifying a given observer from the receiver’s dispatch table.
Declaration
Swift
func removeObserver(_observer: AnyObject)Objective-C
- (void)removeObserver:(id)notificationObserverParameters
notificationObserverThe observer to remove. Must not be
nil.Discussion
Be sure to invoke this method (or
removeObserver:name:object:) beforenotificationObserveror any object specified inaddObserver:selector:name:object:is deallocated.You should not use this method to remove all observers from an object that is going to be long-lived, because your code may not be the only code adding observers that involve the object.
The following example illustrates how to unregister
someObserverfor all notifications for which it had previously registered. This is safe to do in thedeallocmethod, but should not otherwise be used—useremoveObserver:name:object:instead.[[NSNotificationCenter defaultCenter] removeObserver:someObserver];
Availability
Available in OS X v10.0 and later.
See Also
-
Removes matching entries from the receiver’s dispatch table.
Declaration
Objective-C
- (void)removeObserver:(id)notificationObservername:(NSString *)notificationNameobject:(id)notificationSenderParameters
notificationObserverObserver to remove from the dispatch table. Specify an observer to remove only entries for this observer. Must not be
nil, or message will have no effect.notificationNameName of the notification to remove from dispatch table. Specify a notification name to remove only entries that specify this notification name. When
nil, the receiver does not use notification names as criteria for removal.notificationSenderSender to remove from the dispatch table. Specify a notification sender to remove only entries that specify this sender. When
nil, the receiver does not use notification senders as criteria for removal.Discussion
Be sure to invoke this method (or
removeObserver:) before the observer object or any object specified inaddObserver:selector:name:object:is deallocated.Availability
Available in OS X v10.0 and later.
See Also
-
Posts a given notification to the receiver.
Declaration
Swift
func postNotification(_notification: NSNotification)Objective-C
- (void)postNotification:(NSNotification *)notificationParameters
notificationThe notification to post. This value must not be
nil.Discussion
You can create a notification with the
NSNotificationclass methodnotificationWithName:object:ornotificationWithName:object:userInfo:. An exception is raised ifnotificationisnil.Availability
Available in OS X v10.0 and later.
-
Creates a notification with a given name and sender and posts it to the receiver.
Declaration
Objective-C
- (void)postNotificationName:(NSString *)notificationNameobject:(id)notificationSenderParameters
notificationNameThe name of the notification.
notificationSenderThe object posting the notification.
Discussion
This method invokes
postNotificationName:object:userInfo:with auserInfoargument ofnil.Availability
Available in OS X v10.0 and later.
See Also
-
Creates a notification with a given name, sender, and information and posts it to the receiver.
Declaration
Swift
func postNotificationName(_aName: String, objectanObject: AnyObject?, userInfoaUserInfo: [NSObject : AnyObject]?)Objective-C
- (void)postNotificationName:(NSString *)notificationNameobject:(id)notificationSenderuserInfo:(NSDictionary *)userInfoParameters
notificationNameThe name of the notification.
notificationSenderThe object posting the notification.
userInfoInformation about the the notification. May be
nil.Discussion
This method is the preferred method for posting notifications.
Availability
Available in OS X v10.0 and later.
See Also
Copyright © 2015 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2014-07-15
