NSNotification provides a simple of way of messaging within an iOS application.
Create Notification Observer
Register a function to handle notifications and inside of viewWillAppear or other initializing method.
1 |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"Notification Name" object:nil]; |
Handler Method
Notifications can technically send any object type. Here we simply pass an NSString object:
1 2 3 4 5 |
-(void)handleNotification:(NSNotification *)pNotification { // Handle Notification NSLog(@"Received Notification: %@",(NSString*)[pNotification object]); } |
Remove Notification Observer
Remove observer when views go away viewDidDisappear or viewWillDisappear.
1 |
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification Name" object:self]; |
For more information, see Apple’s developer pages.