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.
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
NSNotificationSuspensionBehaviorDrop
The server does not queue any notifications with this name and object until it receives the setSuspended:NO
message.
NSNotificationSuspensionBehaviorCoalesce
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,NSNotificationSuspensionBehaviorCoalesce
is
the default.
NSNotificationSuspensionBehaviorHold
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.
NSNotificationSuspensionBehaviorDeliverImmediately
The server delivers notifications matching this registration irrespective of whether it has received the setSuspended:YES
message. When a notification with this suspension behavior
is matched, it has the effect of first flushing any queued notifications. The effect is as if the server received setSuspended:NO
while the application is suspended, followed
by the notification in question being delivered, followed by a transition back to the previous suspended or unsuspended state.
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
.
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.