Inherits From:
NSObject
Conforms To:
NSObject (NSObject)
Declared In:
Foundation/NSNotification.h
+ defaultCenter | Accesses the default notification center. |
- addObserver:selector:name:object: | Registers an object to receive a notification. |
- postNotificationName:object: | Creates and posts a notification. |
- removeObserver: | Specifies that an object no longer wants to receive notifications. |
Each task has a default notification center that you access with the defaultCenter
class method.
addObserver:selector:name:object:
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.
postNotification:
. The methods postNotificationName:object:
and postNotificationName:object:userInfo:
are convenient ways to post notifications without having to create an NSNotification first.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(objectAddedToConverterList:)
name:@"ConverterAdded" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(objectRemovedFromConverterList:)
name:@"ConverterRemoved" object:nil];
When a user installs or removes a Converter, the Converter sends one of the following messages to the notification center:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"ConverterAdded" object:self];
or
[[NSNotificationCenter defaultCenter]
postNotificationName:@"ConverterRemoved" object:self];
The notification center identifies all observers who are interested in the "ConverterAdded" or "ConverterRemoved" notifications by invoking the method they specified in the selector argument of addObserver:selector:name:object:
. In the case of our example observer, the selectors are objectAddedToConverterList:
and objectRemovedFromConverterList:
. Assume the Converter class has an instance method converterName
that returns the name of the Converter object. Then the objectAddedToConverterList:
method might have the following implementation:
- (void)objectAddedToConverterList:(NSNotification *)notification
{
Converter *addedConverter = [notification object];
// Add this to our popup (it will only be added if not there)...
[myPopUpButton addItem:[addedConverter converterName]];
}
The Converters don't need to know anything about the pop-up list or any other aspect of the user interface to your program.
If there are other objects of interest to the observer, place them in the notification's optional dictionary or use postNotificationName:object:userInfo:
.
defaultCenter
Returns the current task's notification center, which is used for system notifications.
addObserver:
(id)anObserverselector:
(SEL)aSelectorname:
(NSString *)notificationNameobject:
(id)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 nil
, the notification center notifies the observer of all notifications with an object matching anObject. If anObject is nil
, the notification center notifies the object of all notifications with the name notificationName.
The notification center does not retain anObserver or anObject. Therefore, you should always send removeObserver:
or removeObserver:name:object:
to the notification center before releasing these objects.
postNotification:
(NSNotification *)notification
Posts notification to the notification center. You can create notification with the NSNotification class method notificationWithName:object:
or notificationWithName:object:userInfo:
. An exception is raised if notification is nil
.
See also:
- postNotificationName:object:
, - postNotificationName:object:userInfo:
postNotificationName:
(NSString *)notificationName
object:
(id)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 that is posting the notification. It may be nil
.
This method invokes postNotificationName:object:userInfo:
with a userInfo:
argument of nil
.
See also:
- postNotification:
postNotificationName:
(NSString *)notificationName
object:
(id)anObjectuserInfo:
(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 that is posting the notification. It may be nil
. userInfo also may be nil
.
See also:
- postNotificationName:object:
removeObserver:
(id)anObserver
Removes anObserver from all notification associations in the notification center. Be sure to invoke this method (or removeObserver:name:object:
) before releasing anObserver or any object specified in addObserver:selector:name:object:
.
removeObserver:
(id)anObserver name:
(NSString *)notificationNameobject:
(id)anObject
Removes anObserver as the observer of notifications with the name notificationName and object anObject from the notification center. Be sure to invoke this method (or removeObserver:
) before deallocating the observer object or any object specified in addObserver:selector:name:object:
.
If anObserver is nil
, all objects are removed as observers of notificationName containing anObject. (Recall that the object that a notification contains is usually the object that posted the notification.) If notificationName is nil
, anObserver is removed as an observer of all notifications containing anObject. If anObject is nil
, anObserver is removed as an observer of notificationName containing any object. For example, if you wanted all objects to stop observing notifications containing the object aWindow
, you would sent this message:
[[NSNotificationCenter defaultCenter] removeObserver:nil name:nil object:aWindow];
Copyright © 1997, Apple Computer, Inc. All rights reserved.