Before we talked about iOS different interface between the value of the time, said can be sent through the notification center value. So what is the notification center, and how did he achieve the value of the transfer?
Nsnotificationcenter is a singleton and only provides a unique instantiation of the portal, which only has one instance within the entire application: [nsnotificationcenter defaultcenter]
You can automatically receive messages from other objects through the notification hub as long as you follow the Nsnotificationcenter instance. Because its followers can have many, so it also has a broadcast nature.
In the notification center, the message sender and receiver are completely decoupled , which is excellent.
The essence of notification hubs is the observer pattern, which registers an object (Nsnotificationcenter) for observation, triggering a callback method whenever it changes.
To use notification hubs: Suppose you want to pass a value from a to B
1.B focus on the notification center, you can also say to the Notification Center to register Observer B:
[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector( Urlchange:) name:@ "Newurl" object:nil];
< Span class= "S1" > 2.A send message via notification hub:
< Span class= "S1" > < Span class= "S1" > [[nsnotificationcenter Defaultcenter Postnotificationname:@ "Newurl" Object:nil Userinfo:@{@ "Newsiteurl" :value};
3. Because B is concerned with notification hubs, the callback method is triggered when the notification hub has a message, so execution -(void) Urlchange: (nsnotification *) Notification
Note: Nsnotification is the carrier of the message information, the Notification Center can be obtained through the Notification.userinfo notice information.
4. Remove the viewer:
Overriding destructor Dealloc, removing the viewer in it (to cancel the attention to the Notification center)
[[nsnotificationcenter defaultcenter] removeobserver:self];
Qf--ios Messaging mechanism (notification hubs)