Cocoa Programming for Mac OS X - Aaron Hillegass [72]
Many standard Cocoa classes post notifications: Windows send notifications that they have changed size. When the selection of a table view changes, the table view sends a notification. The notifications sent by standard Cocoa objects are listed in the online documentation.
In our example, you will register all your RMDocument objects as observers. Your preference controller will post a notification when the user chooses a new color. When sent the notification, the RMDocument objects will change the background color.
Before the RMDocument object is deallocated, you must remove it from the notification center’s list of observers. Typically, this is done in the dealloc method. (If you are using garbage collection, the instance of RMDocument will be automatically removed from the notification center when it is deallocated.)
What Notifications Are Not
A notification center allows objects in an application to send notifications to other objects in that same application. When programmers first hear about the notification center, they sometimes think that it is a form of interprocess communications. (“I will create an observer in one application and post notifications from an object in another.”)
Notifications do not travel between applications. (Look into NSDistributedNotificationCenter if you need to pass notifications between applications.)
NSNotification
Notification objects are very simple. A notification is like an envelope into which the poster will place information for the observers. A notification has two important instance variables: name and object. Nearly always, object is a pointer to the object that posted the notification. (It is analogous to a return address.)
Thus, the notification also has two interesting methods:
- (NSString *)name
- (id)object
NSNotificationCenter
The NSNotificationCenter is the brains of the operation. It allows you to do three things: register observer objects, post notifications, and unregister observers.
Here are some commonly used methods implemented by NSNotificationCenter:
+ (NSNotificationCenter *)defaultCenter
Returns the notification center.
- (void)addObserver:(id)anObserver
selector:(SEL)aSelector
name:(NSString *)notificationName
object:(id)anObject
Registers anObserver to receive notifications with the name notificationName and containing anObject (Figure 14.1). When a notification of the name notificationName containing the object anObject is posted, anObserver is sent an aSelector message with this notification as the argument.
Figure 14.1. Registering for Notifications
If notificationName is nil, the notification center sends the observer all notifications with an object matching anObject.
If anObject is nil, the notification center sends the observer all notifications with the name notificationName.
The observer is not retained by the notification center. Note that the method takes a selector.
- (void)postNotification:(NSNotification *)notification
Posts a notification to the notification center (Figure 14.2).
- (void)postNotificationName:(NSString *)aName
object:(id)anObject
Figure 14.2. Posting a Notification
Creates and posts a notification.
- (void)removeObserver:(id)observer
Removes observer from the list of observers.
Posting a Notification
Posting a notification is the easiest step, so you will start there. When it receives a changeBackgroundColor: message, your PreferenceController object will post a notification with the new color.
You are going to name the notification @"BNRColorChanged", but you are going to create a global variable for the constant. (Experienced programmers put a prefix on the notification so that