Next Page > Hide TOC

Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

NSNotificationCenter

Inherits from
Package
com.apple.cocoa.foundation
Companion guide

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.

Overview

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.

Tasks

Constructors

Accessing the Default Center

Adding and Removing Observers

Posting Notifications

Constructors

NSNotificationCenter

Creates an empty NSNotificationCenter.

public NSNotificationCenter()

Discussion

This center is not the default notification center. To obtain the default center, use defaultCenter.

Static Methods

defaultCenter

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

public static NSNotificationCenter defaultCenter()

Instance Methods

addObserver

Registers anObserver to receive notifications with the name notificationName and/or containing anObject.

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

Discussion

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.

postNotification

Posts notification to the receiver.

public void postNotification(NSNotification notification)

Discussion

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)

Discussion

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)

Discussion

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

Removes anObserver from all notification associations in the receiver.

public void removeObserver(Object anObserver)

Discussion

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)

Discussion

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);


Next Page > Hide TOC


© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.