PATH |
NSNotificationCenter
- Inherits from:
- Object
- Package:
- com.webobjects.foundation
Class Description
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.
Registering to Receive Notifications
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.
Method Types
- Constructors
- NSNotificationCenter
- Accessing the default center
- defaultCenter
- Adding and removing observers
- addObserver
- addOmniscientObserver
- removeObserver
- removeOmniscientObserver
- Posting notifications
- postNotification
Constructors
NSNotificationCenter
protected NSNotificationCenter()
Static Methods
defaultCenter
public static NSNotificationCenter defaultCenter()
Instance Methods
addObserver
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. |
addOmniscientObserver
public synchronized void addOmniscientObserver( Object anObserver, NSSelector aSelector)
postNotification
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)
removeObserver
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. |
Recall that the object a notification contains is usually the object that posted the notification.
removeOmniscientObserver
public synchronized void removeOmniscientObserver(Object anObserver)
toString
public String toString()
© 2001 Apple Computer, Inc. (Last Published April 17, 2001)