Here is note for NSNotificationCenter if you have any new tip on NSNotificationCenter then please write in comment so i'll add in note so it'll useful for other.If you have any tip to use NSNotificationCenter then also share we me.Here is simple way to implement Notifications.
Notifications are an incredibly useful way to send messages (and data) between objects that otherwise don't know about each other. Think of it like a radio station broadcasting messages: the station (sender) broadcasts the message, and listeners can choose to listen in and receive some (or all) of the broadcast messages.
For example, you might have a network reachability check in your app delegate, and post notifications whenever the reachability changes. Other objects would listen for that notification and react accordingly when the network goes up or down.
Some Cocoa frameworks send notifications when certain events happen. In iOS 4.0 and up, the application sends notifications when an app is about to move the background... and when it returns from the background. Your app's objects and controllers can listen for these notifications and stop actions (or save data) when the app is about to quit, and resume when it becomes active again.
Normal Notification.
Step 1 : Register your Notification in class in which you want to use. Unregister its in same class when class is no longer.
Register
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodName) name:@"myNotificationFire" object:nil];
Unregister - Unresister it in class dealloc method or when notification is not in use.
[[NSNotificationCenter defaultCenter] removeObserver:self];
Step 2 : Implement its register method
-(void) methodName{
NSLog(@"Notification Method Call");
}
Step 3 : Use that Notification.
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotificationFire" object:nil]
Notification with object.
Step 1 : Register your Notification in class in which you want to use. Unregister its in same class when class is no longer.
Register
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodName:) name:@"myNotificationFire" object:nil];
UnRegister - Unresister it in class dealloc method or when notification is not in use.
[[NSNotificationCenter defaultCenter] removeObserver:self];
Step 2 : Implement its register method
-(void) methodName:(NSNotification *)notification{
NSMutableDictionary *dictionary =(NSMutableDictionary*) [notification object];
NSLog(@"Notification Method Call.And object content is :%@", dictionary);
}
Step 3 : Use that Notification.
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotificationFire" object:ObjectName]
//ObjectName that is object that you want to receive at notification receiver method. In our case its "methodName"
Additional References
Here is Apple Official document list for notification.NSNotificationCenter
If any tip or new idea or change for iOs 7 NSNotificationCenter then please share with us.
Hope this will useful to you. If any suggestion then please write in comment . Thank You.
No comments:
Post a Comment