Next Page > Hide TOC

Legacy Documentclose button

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

NSNotification

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

Overview

NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object. An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any. NSNotification objects are immutable objects.

You can create a notification object with the constructor. However, you don’t usually create your own notifications directly. The NSNotificationCenter method postNotification allows you to conveniently post a notification without creating it first.

Creating Subclasses

You can subclass NSNotification to contain information in addition to the notification name, object, and dictionary. This extra data must be agreed upon between notifiers and observers.

Tasks

Constructors

Obtaining Information About a Notification

Constructors

NSNotification

Throws an IllegalArgumentException.

public NSNotification()

Discussion

Use one of the other constructors to create an instance.

Availability

Creates a notification object that associates the name aName with the object anObject.

public NSNotification(String aName, Object anObject)

Discussion

aName may not be null.

Creates a notification object that associates the name aName with the object anObject and the dictionary of arbitrary data userInfo.

public NSNotification(String aName, Object anObject, NSDictionary userInfo)

Discussion

The dictionary userInfo may be null. aName may not be null.

See Also

Instance Methods

name

Returns the name of the receiver.

public String name()

Discussion

Examples of this might be “PortIsInvalid” or “PhoneRinging.” Typically, you invoke this method on the notification object passed to your notification-handler method. (You specify a notification-handler method when you register to receive the notification.)

Notification names can be any string. To avoid name collisions, however, you might want to use a prefix that’s specific to your application.

object

Returns the object associated with the receiver.

public Object object()

Discussion

This is often the object that posted this notification. It may be null.

Typically, you invoke this method on the notification object passed in to your notification-handler method. (You specify a notification-handler method when you register to receive the notification.)

userInfo

Returns the NSDictionary associated with the receiver or null if there is no such object.

public NSDictionary userInfo()

Discussion

The NSDictionary stores any additional objects that objects receiving the notification might use. For example, in the Application Kit, NSControl objects post the ControlTextDidChangeNotification whenever the field editor (an NSText object) changes text inside the NSControl. This notification provides both the NSControl object and the field editor to objects registered to receive them. The field editor is returned when you access the dictionary.



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.