Advertisements
NSNotification by example
Creating Notifications
+ notificationWithName:object:
Returns a new notification object with a specified name and object.
Example
NSNotification *notification = [NSNotification notificationWithName:@"Eezy" object:@"my object"]; [[NSNotificationCenter defaultCenter]postNotification:notification]; NSLog(@"%@",notification.name);
Output
2014-04-12 20:59:41.948 iOS-Tutorial[2627:a0b] Eezy
+ notificationWithName:object:userInfo:
Returns a notification object with a specified name, object, and user information.
Example
NSNotification *notification = [NSNotification notificationWithName:@"Eezy" object:@"my object" userInfo:@{@"Status": @"Success"}]; [[NSNotificationCenter defaultCenter]postNotification:notification]; NSLog(@"%@",notification.name);
Output
2014-04-12 21:02:58.555 iOS-Tutorial[2666:a0b] Eezy
- initWithName:object:userInfo:
Initializes a notification with a specified name, object, and user information.
Example
NSNotification *notification = [[NSNotification alloc] initWithName:@"Eezy" object:@"my object" userInfo:@{@"Status": @"Success"}]; [[NSNotificationCenter defaultCenter]postNotification:notification]; NSLog(@"%@",notification.name);
Output
2014-04-12 21:02:58.555 iOS-Tutorial[2666:a0b] Eezy
Getting Notification Information
- name
Returns the name of the notification.
Example
NSNotification *notification = [[NSNotification alloc] initWithName:@"Eezy" object:@"my object" userInfo:@{@"Status": @"Success"}]; [[NSNotificationCenter defaultCenter]postNotification:notification]; NSLog(@"%@",[notification name]);
Output
2014-04-12 21:02:58.555 iOS-Tutorial[2666:a0b] Eezy
- object
Returns the object associated with the notification.
Example
NSNotification *notification = [[NSNotification alloc] initWithName:@"Eezy" object:@"my object" userInfo:@{@"Status": @"Success"}]; [[NSNotificationCenter defaultCenter]postNotification:notification]; NSLog(@"%@",[notification object]);
Output
2014-04-12 21:04:54.347 iOS-Tutorial[2685:a0b] my object
- userInfo
Returns the user information dictionary associated with the receiver.
Example
NSNotification *notification = [[NSNotification alloc] initWithName:@"Eezy" object:@"my object" userInfo:@{@"Status": @"Success"}]; [[NSNotificationCenter defaultCenter]postNotification:notification]; NSLog(@"%@",[notification userInfo]);
Output
2014-04-12 21:05:25.221 iOS-Tutorial[2698:a0b] { Status = Success; }
Advertisements