Registration: [[Nsnotificationcenter Defaultcenter] postnotificationname:@ "ChangeColor" object:self];
Reception observation: [[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (changecolor) name:@ "ChangeColor" Object:nil];
Remove the observer at Addobserver :
-(void) dealloc
{
[[Nsnotificationcenter defaultcenter]removeobserver:self];
}
Notifications are a typical observer pattern in iOS.
Observer mode in 1.iOS: notification, KVO;
2. Differences between notifications and agents:
Personally, the notice is the Observer mode, agent is the proxy mode, so the difference between the notification and agent is the difference between the two modes.
The ① observer pattern is one-to-many, mostly used for the ambiguous observer;
② notification hubs is actually a mechanism for providing message broadcasts within a program. Notification hubs cannot communicate between processes. is actually a setter that forwards the received message to the desired object, based on a message in the inner part. Notification hubs is based on the observer pattern, which allows you to register and delete observers.
A nsnotificationcenter can have a lot of notification messages nsnotification, for each nsnotification can have a lot of observers observer to receive notifications.
The principal agent (Degegate), as the name implies, entrusts something to other objects to do. Then the other object is the agent of this object, instead of it to take care of what to do . Reflected in the program, the first thing to know is the object of the client is which object, the delegate is what the content.
If there is too much notification, the management of the notification is complex, and if it is poorly managed, you will receive inexplicable messages that cannot be traced.
Use of iOS notifications