PATH  Documentation > Mac OS X > Foundation Reference: Java



Table of Contents

NSNotificationCenter


Inherits from:
NSObject
Package:
com.apple.yellow.foundation

Class at a Glance


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.

Principal Attributes


Creation


Each task has a default notification center. You typically don't create your own.

Commonly Used Methods



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.


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.


Registering to Receive Notifications


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
removeObserver
Posting notifications
postNotification


Constructors



NSNotificationCenter

public NSNotificationCenter()

Description forthcoming.


Static Methods



defaultCenter

public static NSNotificationCenter defaultCenter()

Returns the current task's notification center, which is used for system notifications.


Instance Methods



addObserver

public void addObserver( Object anObserver, NSSelector aSelector, String notificationName, Object anObject)

Registers anObserver to receive notifications with the name notificationName and/or containing 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. If anObject is null, the notification center notifies the observer of all notifications with the name notificationName.

postNotification

public void postNotification(NSNotification notification)

Posts notification to the notification center. You can create notification with the NSNotification constructor. An exception is thrown if notification is null.

public void postNotification( String notificationName, Object anObject)

Creates a notification with the name notificationName, associates it with the object anObject, and posts it to the notification center. anObject is typically the object posting the notification. It may be null.

public void postNotification( String notificationName, Object anObject, NSDictionary userInfo)

Creates a notification with the name notificationName, associates it with the object anObject and dictionary userInfo, and posts it to the notification center.

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.



removeObserver

public void removeObserver(Object anObserver)

Removes anObserver from all notification associations in the notification center.

public void removeObserver( Object anObserver, String notificationName, Object anObject)

Removes anObserver as the observer of notifications with the name notificationName and object anObject from the notification center.

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.




Table of Contents