Let's talk about small examples in our daily life. When we are still young, We need to drink milk. In order to drink fresh milk, we often go to a store dedicated to ordering milk and put our residential address, the brand of the purchased milk also tells the person in charge of the store that we can drink fresh milk every day. Well, we don't need to go anywhere, the person in charge of the store will personally give you the milk.
We can also find that this can also avoid direct connection between "Producers" (milk manufacturers) and "consumers" (US), which is actually to reduce the coupling between the two modules. The milk manufacturer delivers the milk of each brand to the store where the milk is sold, and then transmits the milk to our "consumer" through the consumer's ordering information (milk brand, residential address.
In IOS development, you can use notifications to implement this function. If you have the opportunity to contact KVO in the future, let's make a conclusion.
Registration notification
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (sendMilk :) name: @ "quechao" object: nil];
A store that subscribes to milk is like a notification center in IOS. It registers a notification named quechao and uses self as the observer, that is, when a message named quechao is sent (the manufacturer sends the quechao-branded milk to the store), The sendMilk method is executed to send the milk to the consumer.
Send message
[[Nsicationcenter center defacenter center] postNotificationName: @ "quechao" object: milk]; The manufacturer sends the quecao-branded milk to the store. After receiving the notification, execute sendMilk: Method
-(Void) sendMilk :( NSNotification *) notification
{
// Code
}
The advantage of the notification method is that it separates the relationship between the producer and the consumer. But in this case, it is often too bad, just like this, if there is no connection at all, it will easily lead to poor code readability. When there are many notifications, reading may be complicated and there is no idea.