Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [73]

By Root 838 0
it doesn’t conflict with other notifications that may be flying around the application.) Open PreferenceController.h and add the declaration with the other string constants:

extern NSString * const BNRColorChangedNotification;

In PreferenceController.m, define the constant:

NSString * const BNRColorChangedNotification = @"BNRColorChanged";

Make your changeBackgroundColor: method in PreferenceController.m look like this:

- (IBAction)changeBackgroundColor:(id)sender

{

NSColor *color = [colorWell color];

[PreferenceController setPreferenceTableBgColor:color];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

NSLog(@"Sending notification");

[nc postNotificationName:BNRColorChangedNotification object:self];

}

Registering as an Observer


To register as an observer, you must supply the object that is the observer, the name of the notification in which it is interested, and the message that you want sent when an interesting notification arrives. You can also specify that you are interested only in notifications with a certain object attached to them. (Remember that this is often the object that posted the notification. Thus, when you specify that you want resize notifications with a certain window attached, you are saying that you are interested only in the resizing of that particular window.)

Edit your RMDocument class’s init method as follows:

- (id)init

{

self = [super init];

if (self) {

employees = [[NSMutableArray alloc] init];

NSNotificationCenter *nc =

[NSNotificationCenter defaultCenter];

[nc addObserver:self

selector:@selector(handleColorChange:)

name:BNRColorChangedNotification

object:nil];

NSLog(@"Registered with notification center");

}

return self;

}

Then implement dealloc to remove the instance of RMDocument from the notification center:

- (void)dealloc

{

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

Handling the Notification When It Arrives


When the notification arrives, the method handleColorChange: is called. For now, just log its arrival. Add this method to your RMDocument.m file:

- (void)handleColorChange:(NSNotification *)note

{

NSLog(@"Received notification: %@", note);

}

Build and run the application. Note that the notifications are sent and received when the color is edited in the Preferences panel.

The userInfo Dictionary


If you wanted to include more than just the poster with the notification, you would use the userInfo dictionary. Every notification has a variable called userInfo that can be attached to an NSDictionary filled with other information that you want to pass to the observers. In this case, we want to add the color to the userInfo dictionary. RMDocument will use the color when the notification arrives. In PreferenceController.m, add a userInfo dictionary to the notification:

- (IBAction)changeBackgroundColor:(id)sender

{

NSColor *color = [sender color];

[PreferenceController setPreferenceTableBgColor:color];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

NSLog(@"Sending notification");

NSDictionary *d = [NSDictionary dictionaryWithObject:color

forKey:@"color"];

[nc postNotificationName:BNRColorChangedNotification

object:self

userInfo:d];

}

In RMDocument.m, read the color out of the userInfo dictionary:

- (void)handleColorChange:(NSNotification *)note

{

NSLog(@"Received notification: %@", note);

NSColor *color = [[note userInfo] objectForKey:@"color"];

[tableView setBackgroundColor:color];

}

Open several windows and change the preferred background color. Note that all of them receive the notification and change color immediately.

For the More Curious: Delegates and Notifications


An object that has made itself the delegate of a standard Cocoa object is probably interested in receiving notifications from that object as well. For example, if you have implemented a delegate to handle the windowShouldClose: delegate method for a window, that same object is likely to be interested in the NSWindowDidResizeNotification

Return Main Page Previous Page Next Page

®Online Book Reader