Getting the Notification Center

+ defaultCenter

Returns the process’s default notification center.

Example

 NSLog(@"%@",[NSNotificationCenter defaultCenter]);
		

Output

2014-04-12 21:07:57.419 iOS-Tutorial[2727:a0b] 
		

Managing Notification Observers

- addObserverForName:object:queue:usingBlock:

Adds an entry to the receiver’s dispatch table with a notification queue and a block to add to the queue, and optional criteria: notification name and sender.

Example

NSNotification *notification = [[NSNotification alloc] initWithName:@"Eezy" object:@"my object" userInfo:@{@"Status": @"Success"}];
 NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
[[NSNotificationCenter defaultCenter] addObserverForName:@"Eezy" object:@"my object" queue:operationQueue usingBlock:^(NSNotification *note) {
    NSLog(@"%@",note.name);
}];
[[NSNotificationCenter defaultCenter]postNotification:notification];
		

Output

2014-04-12 21:14:53.843 iOS-Tutorial[2791:1403] Eezy
		

- addObserver:selector:name:object:

Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.

Example

- (void)testNotification{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationHandler:) name:@"Eezy" object:@"my object"];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"Eezy" object:@"my object" userInfo:@{@"Status": @"Success"}];
}

- (void)notificationHandler:(NSNotification *) notification{
     NSLog(@"%@",notification.object);
}
		

Output

2014-04-12 21:32:25.389 iOS-Tutorial[2957:a0b] my object
		

- removeObserver:

Removes all the entries specifying a given observer from the receiver’s dispatch table.

Example

[[NSNotificationCenter defaultCenter]removeObserver:self];
		

- removeObserver:name:object:

Removes matching entries from the receiver’s dispatch table.

Example

[[NSNotificationCenter defaultCenter]removeObserver:self name:@"Eezy" object:nil];		

Posting Notifications

- postNotification:

Posts a given notification to the receiver.

Example

NSNotification *notification = [[NSNotification alloc] initWithName:@"Eezy" object:@"my object" userInfo:@{@"Status": @"Success"}];
 NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
[[NSNotificationCenter defaultCenter] addObserverForName:@"Eezy" object:@"my object" queue:operationQueue usingBlock:^(NSNotification *note) {
    NSLog(@"%@",note.name);
}];
[[NSNotificationCenter defaultCenter]postNotification:notification];
		

Output

2014-04-12 21:14:53.843 iOS-Tutorial[2791:1403] Eezy
		

- postNotificationName:object:

Creates a notification with a given name and sender and posts it to the receiver.

Example

NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
[[NSNotificationCenter defaultCenter] addObserverForName:@"Eezy" object:@"my object" queue:operationQueue usingBlock:^(NSNotification *note) {
    NSLog(@"%@",note.object);
}];
[[NSNotificationCenter defaultCenter]postNotificationName:@"Eezy" object:@"my object" ];
		

Output

2014-04-12 21:18:35.981 iOS-Tutorial[2826:1403] my object
		

- postNotificationName:object:userInfo:

Creates a notification with a given name, sender, and information and posts it to the receiver.

Example

NSOperationQueue *operationQueue = [[NSOperationQueue alloc]init];
[[NSNotificationCenter defaultCenter] addObserverForName:@"Eezy" object:@"my object" queue:operationQueue usingBlock:^(NSNotification *note) {
    NSLog(@"%@",note.userInfo);
}];
[[NSNotificationCenter defaultCenter]postNotificationName:@"Eezy" object:@"my object" userInfo:@{@"Status": @"Success"}];
		

Output

2014-04-12 21:19:27.392 iOS-Tutorial[2846:1403] {
    Status = Success;
}
		

Advertisements