Notification

an object can post notification about what is going on to a centralized notification center. Interested objects register to receive a message when a particular notification is posted or when a particular object posts.

Notification center

Every application has an instance of NSNotificationCenter, which works like a smart bulletin board.

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self // The object self will be sent
       selector:@selector(retrieveDog:) // retrieveDog:
           name:@"LostDog" // when @"LostDog" is posted
         object:nil]; // by any object.

Note the nil works as a wildcard in the notification center word.

The method that is triggered when the notification arrives takes an NSNotification object as the argument.

- (void) retrieveDog: (NSNotification *)note
{
    id poster = [note object];
    NSString *name = [note name];
    NSDictionary *extraInfo = [note userInfo];
    ...
}

post example

NSDictionary *extraInfo = ...;
NSNotification *note = [NSNotification notificationWithName:@"LostDog"
                                                     object:self
                                                   userInfo:extraInfo];
[[NSNotificationCenter defaultCenter] postNotification:note];

Note the notification center does not retain observers.

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}

Note

Posting a notification to an NSNotificationCenter is synchronous.(time coupled!)What that means is that when you post a notification with -postNotification: or any of the related NSNotificationCenter methods, the notification is delivered to all appropriate registered observers before -postNotification: returns control to your code.The synchronous behavior also means that you should be mindful of the consequences when you implement methods to react to notifications. If you perform lengthy operations in your notification handling code, you delay the receipt of the notification by other objects and delay return to the code that posted the notification.

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{ // schedule a future message and return from the method
[self performSelector:@selector(doComplexProcessing:)
withObject:[aNotification object] afterDelay:0.0f];
}
- (void)doComplexProcessing:(id)anObject
{ // Do some complex processing based on anObject
}

 

When you need more complex asynchronous behavior than just delaying a message, use Cocoa’s NSNotificationQueue class. NSNotificationQueue instances implement an asynchronous First In First Out (FIFO) queue

UIDevice Notifications

One object that regularly post notifications is UIDevice. Here are the constants that serve as the names of the notifications that a UIDevice posts:

UIDeviceOrientationDidChangeNotification
UIDeviceBatteryStateDidChangeNotification
UIDeviceBatteryLevelDidChangeNotification
UIDeviceProximityStateDidChangeNotification

 

 

posted on 2012-06-16 00:36  grep  阅读(446)  评论(0编辑  收藏  举报