Reputation: 1
I don't know how to use NSNotification in our iphone application. and one more doubt of difference between delegate and NSNotification because both are communicating through objects.
and give the practical example.
Upvotes: 0
Views: 1407
Reputation: 303
=> NSNotificationCenter provides a centralized hub through which any part of an application may notify and be notified of changes from any other part of the application.
=> Observers register with a notification center to respond to particular events with a specified action.
=> Each time an event occurs, the notification goes through its dispatch table, and messages any registered observers for that event.
Use Of NS-Notification in Objective C
//Write from where you want to pass the data
[[NSNotificationCenter defaultCenter]postNotificationName:@"TeamTable" object:hdImage userInfo:nil];
Here
**TeamTable is notification observer name (Unique name)
**hdImage is what data you want to pass to another controller
Now write these code in that Controller from where you want to receive the data
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(detailsData:) name:@"TeamTable" object:nil];
}
-(void)detailsData:(NSNotification*)sender{
//In sender it contain All received data
}
It’s important for objects to remove observers before they’re deallocated, in order to prevent further messages from being sent.
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"TeamTable" object:nil];
}
For More Details about NS-Notification You can follow this Link http://nshipster.com/nsnotification-and-nsnotificationcenter/
Upvotes: 2