Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSNotification.h |
Related sample code |
The NSNotificationCenter
class provides a way to send notifications to objects in the same task. It takes NSNotification
objects and broadcasts them to any objects in the same task that have registered to receive the notification with the task’s default notification center.
Notification dispatch table. Each entry in this table specifies a notification set for a particular observer. A notification set is a subset of the notifications posted to the notification center. Each table entry contains three items:
Notification observer: Required. The object to be notified when qualifying notifications are posted to the notification center.
Notification name: Optional. Specifying a name reduces the set of notifications the entry specifies to those that have this name.
Notification sender: Optional. Specifying a sender reduces the set of notifications the entry specifies to those sent by this object.
Table 1 shows the four types of dispatch table entries and the notification sets they specify. (This table omits the always present notification observer.)
Notification name | Notification sender | Notification set specified |
---|---|---|
Specified | Specified | Notifications with a particular name from a specific sender. |
Specified | Unspecified | Notifications with a particular name by any sender. |
Unspecified | Specified | Notifications posted by a specific sender. |
Unspecified | Unspecified | All notifications. |
Table 2 shows an example dispatch table with four observers.
Observer | Notification name | Notification sender |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
When notifications are posted to the notification center, each of the observers in Table 2 are notified of the following notifications:
observerA
: Notifications named NSFileHandleReadCompletionNotification
.
observerB
: Notifications sent by addressTableView
.
observerC
: Notifications named NSWindowDidChangeScreenNotification
sent by documentWindow
and notifications sent by addressTableView
.
observerD
: All notifications.
defaultCenter
Returns the task’s default notification center.
addObserver:selector:name:object:
Adds an entry to the notification center’s dispatch table specifying at least an observer and a notification message.
postNotificationName:object:
Creates and posts a notification to the notification center.
removeObserver:
Removes all entries from the notification center’s dispatch center that specify a particular observer, so that it no longer receives notifications posted to that notification center.
An NSNotificationCenter
object (or simply, notification center) provides a mechanism for broadcasting information within a task. 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:
method. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling addObserver:selector:name:object:
several times.
When an object (known as the notification sender) posts a notification, it sends an NSNotification
object to the notification center. The notification center then notifies any observers for which the notification meets the criteria specified on registration by sending them the specified notification message, passing the notification as the sole argument. The order in which observers receive notifications is undefined. It is possible for the posting object and the observing object to be the same.
A notification center delivers notifications to observers synchronously. In other words, the postNotification:
methods do not return until all observers have received and processed the notification. To send notifications asynchronously use NSNotificationQueue
. In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself.
Important: The notification center does not retain its observers, therefore, you must ensure that you unregister observers (using removeObserver:
or removeObserver:name:object:
) before they are deallocated. (If you don't, you will generate a runtime error if the center sends a message to a freed object.)
Each task has a default notification center. You typically don’t create your own. An NSNotificationCenter
object can deliver notifications only within a single task. If you want to post a notification to other tasks or receive notifications from other tasks, use a NSDistributedNotificationCenter
object.
Returns the task’s default notification center.
+ (id)defaultCenter
The current task’s default notification center, which is used for system notifications.
NSNotification.h
Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.
- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
Object registering as an observer. Must not be nil
.
Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method the selector specifies must have one and only one argument.
The name of the notification for which to register the observer; that is, only notifications with this name are delivered to the observer. When nil
, the notification center doesn’t use a notification’s name to decide whether to deliver it to the observer.
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. When nil
, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
NSNotification.h
Posts a given notification to the receiver.
- (void)postNotification:(NSNotification *)notification
The notification to post. This value must not be nil
.
You can create a notification with the NSNotification
class method notificationWithName:object:
or notificationWithName:object:userInfo:
. An exception is raised if notification is nil
.
NSNotification.h
Creates a notification with a given name and sender and posts it to the receiver.
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender
The name of the notification.
The object posting the notification.
This method invokes postNotificationName:object:userInfo:
with a userInfo argument of nil
.
NSNotification.h
Creates a notification with a given name, sender, and information and posts it to the receiver.
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo
The name of the notification.
The object posting the notification.
Information about the the notification. May be nil
.
This method is the preferred method for posting notifications.
NSNotification.h
Removes all the entries specifying a given observer from the receiver’s dispatch table.
- (void)removeObserver:(id)notificationObserver
The observer to remove. Must not be nil
.
Be sure to invoke this method (or removeObserver:name:object:
) before notificationObserver or any object specified in addObserver:selector:name:object:
is deallocated.
The following example illustrates how to unregister someObserver
for all notifications for which it had previously registered:
[[NSNotificationCenter defaultCenter] removeObserver:someObserver]; |
NSNotification.h
Removes matching entries from the receiver’s dispatch table.
- (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender
Observer 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.
Name 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.
Sender 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.
Be sure to invoke this method (or removeObserver:
) before the observer object or any object specified in addObserver:selector:name:object:
is deallocated.
NSNotification.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-07-11)