PATH |
- Inherits from:
- Object
- Package:
- com.webobjects.foundation
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.
NSNotificationCenter is implemented using weak references (see Sun's documentation for java.lang.ref.* for details). Thus, if the default NSNotificationCenter is the last object in your application with a reference to either an object registered to receive notifications or an object being observed, the object will be garbage collected.
There are two ways to register to receive notifications.
If an object wishes to register itself to receive all notifications from all objects, it should send the addOmniscientObserver method, specifying the message the notification should send.
Otherwise, 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
- addOmniscientObserver
- removeObserver
- removeOmniscientObserver
- Posting notifications
- postNotification
protected NSNotificationCenter()
public static NSNotificationCenter defaultCenter()
public synchronized void addObserver( Object anObserver, NSSelector aSelector, String notificationName, Object anObject)
null
, but not both:anObject | notificationName | Action |
null |
notificationName | The notification center notifies the observer of all notifications with the name notificationName. |
anObject | null |
The notification center notifies the observer of all notifications with an object matching anObject. |
null |
null | Do not invoke this method specifying null for both notificationName and anObject. Instead, use addOmniscientObserver. |
public synchronized void addOmniscientObserver( Object anObserver, NSSelector aSelector)
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 postNotification( String notificationName, Object anObject)
null
.
public void postNotification(NSNotification notification)
public void removeObserver(Object anObserver)
removeObserver(anObserver, null, null)
.
public synchronized void removeObserver( Object anObserver, String notificationName, Object anObject)
null
parameters as wildcards: removeObserver Parameters | Action |
null , notificationName, anObject |
Removes all observers of notificationName containing anObject. |
anObserver, null , anObject |
Removes anObserver as an observer of all notifications containing anObject. |
anObserver, notificationName, null |
Removes anObserver as an observer of notificationName containing any object. |
anObserver, null , null |
Removes all notifications containing anObserver. |
null , notificationName, null |
Removes all observers of notificationName. |
null , null , anObject |
Removes all observers of anObject. |
public synchronized void removeOmniscientObserver(Object anObserver)
public String toString()
© 2001 Apple Computer, Inc. (Last Published April 17, 2001)