Registering for a Notification
You can register for notifications from within your own application or other applications. See “Registering forLocal Notifications” (page 12) for the former and “Registering for Distributed Notifications” (page 13) for thelatter. To unregister for a notification, which must be done when your object is deallocated, see “Unregisteringan Observer” (page 15).
注册一个通知
你可以在你应用内注册通知,也可以在其他应用里。对于前者或者后者你可以参考相应的文档。当你的对象被释放时,你才可以解除注册通知,你可以参考相应的文档。
Registering for Local Notifications
注册本地通知
You register an object to receive a notification by invoking the notification center method addObserver:selector:name:object:, specifying the observer, the message the notification center should send to the observer, the name of the notification it wants to receive, and about which object. You don't need to specify both the name and the object. If you specify only an object, the observer will receive all notifications containing that object. If you specify only a notification name, the observer will receive that notification every time it’s posted, regardless of the object associated with it.
你可以调用通知中心的方法 addObserver:selector:name:object:,指定观察者,通知中心应该发给观察者的消息,通知的名称,以及关于哪个对象。你不需要同时指定名字和对象。如果你只指定一个对象,观察者将会接收所有的包含那个对象的通知。如果你只指定一个通知名称,那么观察者每次都会接收通知,无视与他相关的对象。
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 can not be determined.
有可能,对于同一个通知,观察者会注册接收不只一个消息。在这种情况下,观察者将接收所有的消息,但是无法决定接收消息的顺序。
If you later decide an observer no longer needs to receive notifications (for example, if the observer is being deallocated), you can remove the observer from the notification center’s list of observers with the methods removeObserver: or removeObserver:name:object:.
如果你后来不想让观察者继续接收通知了(比如,观察者被释放了),你可以通过 removeObserver: or removeObserver:name:object:.把观察者从通知中心列表中移走
Normally, you register objects with the process’s default notification center. You obtain the default object using the defaultCenter class method.
通常,你使用 进程的默认通知中心注册对象。你用defaultCenter类方法获取默认对象。
As an example of using the notification center to receive notifications, suppose you want to perform an operation any time a window becomes main (for example, if you’re implementing a controller for an inspector panel). You would register your client object as an observer as shown in the following example:
下面是个例子
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(aWindowBecameMain:)
name:NSWindowDidBecomeMainNotification object:nil];
2009-08-18 | Copyright © 2009 Apple Inc. All Rights Reserved.12
Registering for a Notification
Registering for Distributed Notifications
By passing nil as the object to observe, the client object (self) is notified when any object posts aNSWindowDidBecomeMainNotification notification.
因为object参数被传递了nil,所以任何对象抛出这个通知时客户对象(self)将会被通知
When window becomes main, it posts an NSWindowDidBecomeMainNotification to the notification center.The notification center notifies all observers who are interested in the notification by invoking the method they specified in the selector argument of addObserver:selector:name:object:. In the case of our example observer, the selector is aWindowBecameMain:. The aWindowBecameMain: method might have the following implementation:
- (void)aWindowBecameMain:(NSNotification *)notification {
NSWindow *theWindow = [notification object];
MyDocument = (MyDocument *)[[theWindow windowController] document];
// Retrieve information about the document and update the panel.
}
The NSWindow objects don’t need to know anything about your inspector panel.
Registering for Distributed Notifications
An object registers itself to receive a notification by sending the addObserver:selector:name:object:suspensionBehavior:
method to an NSDistributedNotificationCenter
object, specifying the message the notification should send, the name of the notification it wants to receive, the identifying string to match (the object argument), and the behavior to follow if notification delivery is suspended.
Because the posting object and the observer may be in different processes, notifications can’t contain pointers to arbitrary objects. Therefore, the NSDistributedNotificationCenter
class requires notifications to use an NSString
object as the object argument. Notification matching is done based on this string, rather than an object pointer. You should check the documentation of the object posting the notification to see what it uses as its identifying string.
When a process is no longer interested in receiving notifications immediately, it may suspend notification delivery. This is often done when the application is hidden, or is put into the background. (The NSApplication
object automatically suspends delivery when the application is not active.) The suspensionBehavior argument in the addObserver
method identifies how arriving notifications should be handled while delivery is suspended. There are four different types of suspension behavior, each useful in different circumstances.
Suspension Behavior | Description |
---|---|
The server does not queue any notifications with this name and object until it receives the | |
The server queues only the last notification of the specified name and object; earlier notifications are dropped. In cover methods for which suspension behavior is not an explicit argument, | |
The server holds all matching notifications until the queue has been filled (queue size determined by the server) at which point the server may flush queued notifications. | |
The server delivers notifications matching this registration irrespective of whether it has received the |
You suspend notifications by sending setSuspended:YES
to the distributed notification center. While notifications are suspended, the notification server handles notifications destined for the process that suspended notification delivery according to the suspension behavior specified by the observers when they registered to receive notifications. When the process resumes notification delivery, all queued notifications are delivered immediately. In applications using Application Kit, the NSApplication
object automatically suspends notification delivery when the application is not active.
Note that a notification destined for an observer that registered with NSNotificationSuspensionBehaviorDeliverImmediately
, automatically flushes the queue as it is delivered, causing all queued notifications to be delivered at that time as well.
The suspended state can be overridden by the poster of a notification. If the notification is urgent, such as a warning of a server being shut down, the poster can force the notification to be delivered immediately to all observers by posting the notification with theNSDistributedNotificationCenter postNotificationName:object:userInfo:deliverImmediately:
method with thedeliverImmediately argument YES
.
Unregistering an Observer
Before an object that is observing notifications is deallocated, it must tell the notification center to stop sending it notifications. Otherwise, the next notification gets sent to a nonexistent object and the program crashes. You can send the following message to completely remove an object as an observer of local notifications, regardless of how many objects and notifications for which it registered itself:
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
For observers of distributed notifications send:
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self]; |
Use the more specific removeObserver...
methods that specify the notification name and observed object to selectively unregister an object for particular notifications.