Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Package | com.apple.cocoa.foundation |
Companion guide |
NSNotificationCenter provides a way for objects that don’t know about each other to communicate. It receives NSNotification objects and broadcasts them to all interested objects.
A table containing objects that want to receive notifications, the notifications they want to receive, and the objects they want to receive these notifications from.
Each task has a default notification center. You typically don’t create your own.
defaultCenter
Accesses the default notification center.
addObserver
Registers an object to receive a notification.
postNotification
Posts a notification.
removeObserver
Specifies that an object no longer wants to receive notifications.
An NSNotificationCenter object (or simply, notification center) is essentially a notification dispatch table. It notifies all observers of notifications meeting specific criteria. This information is encapsulated in NSNotification objects, also known as notifications. Client objects register themselves with the notification center as observers of specific notifications posted by other objects. When an event occurs, an object posts an appropriate notification to the notification center. The notification center dispatches a message to each registered observer, 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.
An NSNotificationCenter object delivers notifications to observers synchronously. In other words the postNotification
method does 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.
An NSNotificationCenter object can only deliver notifications within a single task. If you want to post a notification to other tasks or receive notifications from other tasks, use NSDistributedNotificationCenter.
Creates an empty NSNotificationCenter.
public NSNotificationCenter
()
This center is not the default notification center. To obtain the default center, use defaultCenter
.
Returns the current task’s notification center, which is used for system notifications.
public static NSNotificationCenter defaultCenter
()
Registers anObserver to receive notifications with the name notificationName and/or containing anObject.
public void addObserver
(Object anObserver, NSSelector aSelector, String notificationName, Object anObject)
When a notification of name notificationName containing the object anObject is posted, anObserver receives an aSelector message with this notification as the argument. The method for the selector specified in aSelector must have one and only one argument. If notificationName is null
, the notification center notifies the observer of all notifications with an object matching anObject. (The objects are matched by comparing their pointers.) If anObject is null
, the notification center notifies the observer of all notifications with the name notificationName. anObserver may not be null
.
Posts notification to the receiver.
public void postNotification
(NSNotification notification)
You can create notification with the NSNotification constructor. An exception is thrown if notification is null
.
Creates a notification with the name notificationName, associates it with the object anObject, and posts it to the notification center.
public void postNotification
(String notificationName, Object anObject)
anObject is typically the object posting the notification. It may be null
.
Creates a notification with the name notificationName, associates it with the object anObject and dictionary userInfo, and posts it to the notification center.
public void postNotification
(String notificationName, Object anObject, NSDictionary userInfo)
This method is the preferred method for posting notifications. anObject is typically the object posting the notification. It may be null
. userInfo also may be null
.
Removes anObserver from all notification associations in the receiver.
public void removeObserver
(Object anObserver)
anObserver may not be null
.
Removes anObserver as the observer of notifications with the name notificationName and object anObject from the receiver.
public void removeObserver
(Object anObserver, String notificationName, Object anObject)
anObserver may not be null
.
If notificationName is null
, anObserver is removed as an observer of all notifications containing anObject. If anObject is null
, anObserver is removed as an observer of notificationName containing any object. For example, if you wanted to unregister someObserver
from all notifications it had previously registered for, you would sent this message:
NSNotificationCenter.defaultCenter().removeObserver(someObserver, null, null); |
© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)