Revision: 23258
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 2, 2010 14:48 by BenClayton
Initial Code
NSNotificationCenter.
Mutliple observers can be notified of events.
notification sent by a specific object...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFooHappened:) name:MyFooNotification object:foo];
set object to nil to receive notifications of that name sent by ANY object.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myFooHappened:) name:MyFooNotification object:nil];
Format for myFooHappened is..
-(void) myFooHappened:(NSNotification*)note {
// [note name] - name of notification [note object] - sender
// [note userInfo] is an NSDictionary of key-value pairs filled by the sender.
}
Note: notifications are posted on the SAME THREAD as they were posted.
MUST unregister from all notifications in your dealloc
-(void) dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Posting a notification..
Define a constant for the name of the notification..
in .h
extern NSString* const MyFooNotification;
in .m
NSString* const MyFooNotification = @"MyFooNotification";
-(void) postFoo {
[[NSNotificationCenter defaultCenter] postNotificationName:MyFooNotification object:self userInfo:[NSDictionary ...]];
Initial URL
Initial Description
Initial Title
Using NSNotificationCenter - posting a notification
Initial Tags
Initial Language
Objective C