- Inherits from:
- NSObject
- Package:
- com.apple.yellow.foundation
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.
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 as observers of specific notifications posted by other objects. When an event occurs, an object posts an appropriate notification to the notification center. (See the NSNotification class specification for more on notifications.) The notification center dispatches a message to each registered observer, passing the notification as the sole argument. It is possible for the posting object and the observing object to be the same.
Each task has a default notification center that you access with the defaultCenter static method.
An object registers itself to receive a notification by sending the addObserver method, specifying the message the notification should send, the name of the notification it wants to receive, and about which object. However, the observer need not specify both the name and the object. If it specifies only the object, it will receive all notifications containing that object. If the object specifies only a notification name, it will receive that notification every time it's posted, regardless of the object associated with it.
It is possible for an observer to register to receive more than one message for the same notification. In such a case, the observer will receive all messages it is registered to receive for the notification, but the order in which it receives them cannot be determined.
- Constructors
- NSNotificationCenter
- Accessing the default center
- defaultCenter
- Adding and removing observers
- addObserver
- removeObserver
- Posting notifications
- postNotification
public NSNotificationCenter()
public static NSNotificationCenter defaultCenter()
public void addObserver(
Object anObserver,
NSSelector aSelector,
String notificationName,
Object anObject)
public void postNotification(NSNotification notification)
public void postNotification(
String notificationName,
Object anObject)
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.
public void removeObserver(Object anObserver)
public void removeObserver(
Object anObserver,
String notificationName,
Object anObject)
If anObserver is null, all objects are removed as observers of notificationName containing anObject . (Recall that the object a notification contains is usually the object that posted the notification.) 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.