开发者

NSnotificationCenter postNotificationName who is the receiver?

开发者 https://www.devze.com 2023-02-10 01:45 出处:网络
The documentation http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Found开发者_JAVA百科ation/Classes/NSNotificationCenter_Class/Reference/Reference.html talks of the sender and no

The documentation http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Found开发者_JAVA百科ation/Classes/NSNotificationCenter_Class/Reference/Reference.html talks of the sender and notification name but where does it mention whom to post this notification to?


You don't post a notification directly to someone. The name of the notification, and sender determine who gets the notification.

Interested objects can subscribe to a notification. When you post a notification, all subscribers who are listening to a notification by that name will get notified. Actually Cocoa notifications can be tweaked at two levels:

  • notification name (string)
  • sender

The class documentation illustrates this clearly.

Here's a little ASCII table from the docs showing who gets notified depending on what notification name and sender were used when created:

Notification name | Notification sender | Notification set specified
--------------------------------------------------------------------
Specified         | Specified           | Notifications with a particular name from a specific sender.
Specified         | Unspecified         | Notifications with a particular name by any sender.
Unspecified       | Specified           | Notifications posted by a specific sender.
Unspecified       | Unspecified         | All notifications.

Unspecified means a nil value was supplied for that field.

Notifications allows for a loosely coupled design as objects are not tied together in their implementations and can work independently off each other.


notification is a broadcasting mechanism. As from the doc, "Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods." i.e., any object interested can register as a listener.

0

精彩评论

暂无评论...
验证码 换一张
取 消