Notifications

Notifications provide a mechanism for distributing information about events within an application. Notifications provide an alternative to messaging as a means for communicating between objects. Messaging requires that the sender of the message know who the receiver is when the message is dispatched. Effectively, the notifications system decouples the message sender from the message receiver. With notifications, a broadcast paradigm is implemented in which objects post notifications to a notification center , which then sends messages to objects (known as observers) which have registered their interest in the type of event, or the originating object.

The notification center is an instance of the NSNotificationCenter class; notifications are instances of the NSNotification class. Every notification object has a name identifying the notification type, an object associated with the notification that provides context for the notification, and an optional userInfo dictionary with which posters may pass additional information. When an observer registers with the notification center, it specifies a method to be invoked in response to the posting of a notification. Upon receiving a notification, the notification center identifies the observers of the specific named notification and invokes a predetermined method in each observer.

To obtain an instance of NSNotificationCenter, use the class method defaultCenter . This returns an application’s default notification center. ...

Get Cocoa in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.