Next Page > Hide TOC

NSNotificationCenter Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in Mac OS X v10.0 and later.
Companion guide
Declared in
NSNotification.h
Related sample code

Class at a Glance

The NSNotificationCenter class provides a way to send notifications to objects in the same task. It takes NSNotification objects and broadcasts them to any objects in the same task that have registered to receive the notification with the task’s default notification center.

Principal Attributes

Commonly Used Methods

defaultCenter

Returns the task’s default notification center.

addObserver:selector:name:object:

Adds an entry to the notification center’s dispatch table specifying at least an observer and a notification message.

postNotificationName:object:

Creates and posts a notification to the notification center.

removeObserver:

Removes all entries from the notification center’s dispatch center that specify a particular observer, so that it no longer receives notifications posted to that notification center.

Overview

An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a task. An NSNotificationCenter object is essentially a notification dispatch table.

Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: method. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling addObserver:selector:name:object: several times.

When an object (known as the notification sender) posts a notification, it sends an NSNotification object to the notification center. The notification center then notifies any observers for which the notification meets the criteria specified on registration by sending them the specified notification message, 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.

A notification center delivers notifications to observers synchronously. In other words, the postNotification: methods do 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.

Important: The notification center does not retain its observers, therefore, you must ensure that you unregister observers (using removeObserver: or removeObserver:name:object:) before they are deallocated. (If you don't, you will generate a runtime error if the center sends a message to a freed object.)

Each task has a default notification center. You typically don’t create your own. An NSNotificationCenter object can deliver notifications only within a single task. If you want to post a notification to other tasks or receive notifications from other tasks, use a NSDistributedNotificationCenter object.

Tasks

Getting the Notification Center

Managing Notification Observers

Posting Notifications

Class Methods

defaultCenter

Returns the task’s default notification center.

+ (id)defaultCenter

Return Value

The current task’s default notification center, which is used for system notifications.

Availability
Related Sample Code
Declared In
NSNotification.h

Instance Methods

addObserver:selector:name:object:

Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.

- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender

Parameters
notificationObserver

Object registering as an observer. Must not be nil.

notificationSelector

Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method the selector specifies must have one and only one argument.

notificationName

The name of the notification for which to register the observer; that is, only notifications with this name are delivered to the observer. When nil, the notification center doesn’t use a notification’s name to decide whether to deliver it to the observer.

notificationSender

The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer. When nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

Availability
See Also
Related Sample Code
Declared In
NSNotification.h

postNotification:

Posts a given notification to the receiver.

- (void)postNotification:(NSNotification *)notification

Parameters
notification

The notification to post. This value must not be nil.

Discussion

You can create a notification with the NSNotification class method notificationWithName:object: or notificationWithName:object:userInfo:. An exception is raised if notification is nil.

Availability
See Also
Related Sample Code
Declared In
NSNotification.h

postNotificationName:object:

Creates a notification with a given name and sender and posts it to the receiver.

- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender

Parameters
notificationName

The name of the notification.

notificationSender

The object posting the notification.

Discussion

This method invokes postNotificationName:object:userInfo: with a userInfo argument of nil.

Availability
See Also
Declared In
NSNotification.h

postNotificationName:object:userInfo:

Creates a notification with a given name, sender, and information and posts it to the receiver.

- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo

Parameters
notificationName

The name of the notification.

notificationSender

The object posting the notification.

userInfo

Information about the the notification. May be nil.

Discussion

This method is the preferred method for posting notifications.

Availability
See Also
Declared In
NSNotification.h

removeObserver:

Removes all the entries specifying a given observer from the receiver’s dispatch table.

- (void)removeObserver:(id)notificationObserver

Parameters
notificationObserver

The observer to remove. Must not be nil.

Discussion

Be sure to invoke this method (or removeObserver:name:object:) before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated.

The following example illustrates how to unregister someObserver for all notifications for which it had previously registered:

[[NSNotificationCenter defaultCenter] removeObserver:someObserver];
Availability
Related Sample Code
Declared In
NSNotification.h

removeObserver:name:object:

Removes matching entries from the receiver’s dispatch table.

- (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender

Parameters
notificationObserver

Observer to remove from the dispatch table. Specify an observer to remove only entries for this observer. Must not be nil, or message will have no effect.

notificationName

Name of the notification to remove from dispatch table. Specify a notification name to remove only entries that specify this notification name. When nil, the receiver does not use notification names as criteria for removal.

notificationSender

Sender to remove from the dispatch table. Specify a notification sender to remove only entries that specify this sender. When nil, the receiver does not use notification senders as criteria for removal.

Discussion

Be sure to invoke this method (or removeObserver:) before the observer object or any object specified in addObserver:selector:name:object: is deallocated.

Availability
Related Sample Code
Declared In
NSNotification.h

Next Page > Hide TOC


© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-07-11)


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.