Transferred from: http://blog.csdn.net/liliangchw/article/details/8276803
The most basic way to communicate between objects is by messaging, and the notification center mechanism is provided in cocoa to accomplish this task. Its primary role is to be responsible for communication between any of the two objects. The use of the method is simple, following a few steps:
Suppose communication between A and B, B to trigger an event, a to accept the event, and to respond.
1) A write a custom message response function update
2) A registers with the message center, [nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (update) name:@ "Update" object: Nil
3) B trigger Event [[Nsnotificationcenter Defaultcenter] postnotificationname:@ "Update" Object:nil]
Each process has a default nsnotificationcenter that can be used by the class method Defaultcenter to get an instance of the message center. The message Center can handle messages between different objects in the same process. If you want to communicate between processes on the same machine, you need to use Nsdistributednotificationcenter.
The Message Center distributes the message to all observers in a synchronized manner, in other words, until all observers have received the message and processed it, control is returned to the caller's hand. If you need to process messages asynchronously, you need to use the notification queue nsnotificationqueue.
In multithreaded programs, notifications are distributed to each thread that initiates the message, which may not be the same thread as the one on which the observer was registered.
1. Define the associated value of the message creation is the flag that finds the method
NSString *const gametoiphonenotification = @ "Gametoiphonenotification"; The value of the gametoiphonenotification variable, @ "Gametoiphonenotification", is stored in the notification hub, which is used by the information center to identify the variable
1. Register a Message Center
Nsnotificationcenter *center = [Nsnotificationcenter defaultcenter];
[Center addobserver:self selector: @selector (ontoiphone:) name:gametoiphonenotification Object:nil];
-(void) Ontoiphone: (nsnotification*) Notify: This method is accepted to gametoiphonenotification the method called by this notification
2. Call Information
Nsnotificationcenter * Center = [Nsnotificationcenter Defaultcenter];
[Center postnotificationname:gametoiphonenotification Object:nil Userinfo:[nsdictionary Dictionarywithobjectsandkeys: [NSNumber numberwithint:smsrecommendnotification], @ "Actcode", Nil]];
[Nsdictionary dictionarywithobjectsandkeys: [NSNumber numberwithint:smsrecommendnotification] This is passed to-(void) Ontoiphone: (nsnotification*) parameter of notify.
How to use iOS development nsnotificationcenter (notification)
iOS software development will encounter this situation: After opening the app will run a method in the background, such as downloading files, after the download may need to call a method to refresh the interface, this time may not be able to callback in the downloaded function. Nsnotificationcenter (notification) is a good choice.
Notifications are very simple to use:
1. Define the method that will be called:
- ( void )callBack{ NSLog(@ "this is Notification." ); } |
2. Define notifications:
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(callBack) name: @ "back" object: nil]; |
3. Call Notification:
-( void      nslog (@ "Get it."      //give notice      [[nsnotificationcenter Defaultcenter] postnotificationname:@ "back" object:self]; } |
4. 移出通知: - ( void )dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:@ "back" object:nil]; [super dealloc]; } |
IOS nsnotificationcenter (Messaging mechanism)