Inherits From:
NSObject
Conforms To:
NSCoding, NSCopying
NSObject (NSObject)
Declared In:
Foundation/NSNotification.h
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.
Any object may post a notification. Other objects can register themselves as observers to receive notifications when they are posted. The object posting the notification, the object included in the notification, and the observer of the notification may all be different objects or the same object. Objects that post notifications need not know anything about the observers. On the other hand, observers need to know at least the notification name and keys to the dictionary if provided.
NSNotification objects are immutable objects.
This notification model frees an object from concern about what objects it should send information to. Any object may simply post a notification without knowing what objects-if any-are receiving the notification. However, objects receiving notifications do need to know at least the notification name if not the type of information the notification contains. The notification center takes care of broadcasting notifications to registered observers. Another benefit of this model is to allow multiple objects to listen for notifications, which would otherwise be cumbersome.
You can create a notification object with the class methods notificationWithName:object:
or notificationWithName:object:userInfo:
. However, you don't usually create your own notifications directly. The NSNotificationCenter methods postNotificationName:object:
and postNotificationName:object:userInfo:
allow you to conveniently post a notification without creating it first.
copy
message to that array, which recursively copies every item.
NSNotification is actually a class cluster. As such, it provides no storage for the name, object, and userInfo values. To subclass NSNotification, you must override the primitive methods name
, object
, and userInfo
. Use the init
method as the designated initializer.
notificationWithName:
(NSString *)aNameobject:
(id)anObject
Returns a notification object that associates the name aName with the object anObject.
This method copies aName and retains anObject.
See also:
- postNotificationName:object:
(NSNotificationCenter)
notificationWithName:
(NSString *)aName
object:
(id)anObjectuserInfo:
(NSDictionary *)userInfo
Returns a notification object that associates the name aName with the object anObject and the dictionary of arbitrary data, userInfo. The dictionary userInfo may be nil
.
This method copies aName and retains both anObject and userInfo.
See also:
+ notificationWithName:object:, - postNotificationName:object:userInfo: (NSNotificationCenter)
name
Returns the name of the notification. 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 this notification. This is often the object that posted this notification. It may be nil
.
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.)
For example, suppose you've registered an object to receive the message handlePortDeath:
when the "PortInvalid" notification is posted to the notification center and that handlePortDeath:
needs to access the object monitoring the port that is now invalid. handlePortDeath:
can retrieve that object as shown here:
- (void)handlePortDeath:(NSNotification *)notification
{
...
[self reclaimResourcesForPort:[notification object]];
...
}
userInfo
Returns the NSDictionary associated with this notification or nil
if there is no such object. The NSDictionary stores any additional objects that objects receiving this notification might use. For example in the Application Kit, NSControl objects post the NSControlTextDidChangeNotification 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 it. The field editor is returned when you access the dictionary, as shown here:
- (void)controlTextDidBeginEditing:(NSNotification *)notification
{
NSText *fieldEditor = [[notification userInfo]
objectForKey:@"NSFieldEditor"];/* the field editor */
NSControl *postingObject = [notification object];
/* The object that posted the notification. */
...
}