简介
通知(NSNotification),是采用“观察者”模式来实现,发送通知的本身不需要知道使用者是谁,也不用知道有几个使用者,只需要在通知中心发送通知就可以实现消息的一个或者多个的传递。
A notification is a message sent to one or more observing objects to inform them of an event in a program. The notification mechanism of Cocoa follows a broadcast model. It is a way for an object that initiates or handles a program event to communicate with any number of objects that want to know about that event. These recipients of the notification, known as observers, can adjust their own appearance, behavior, and state in response to the event. The object sending (or posting) the notification doesn’t have to know what those observers are. Notification is thus a powerful mechanism for attaining coordination and cohesion in a program. It reduces the need for strong dependencies between objects in a program (such dependencies would reduce the reusability of those objects). Many classes of the Foundation, AppKit, and other Objective-C frameworks define notifications that your program can register to observe.
The centerpiece of the notification mechanism is a per-process singleton object known as the notification center (NSNotificationCenter). When an object posts a notification, it goes to the notification center, which acts as a kind of clearing house and broadcast center for notifications. Objects that need to know about an event elsewhere in the application register with the notification center to let it know they want to be notified when that event happens. Although the notification center delivers a notification to its observers synchronously, you can post notifications asynchronously using a notification queue (NSNotificationQueue).
使用
比如现在有一个场景:当在用户成功登陆之后要在需要登陆的页面展示登陆成功后的样式:假设有个人中心控制器:ProfileViewController,登陆控制器:LoginViewController。在登录成功之后改变ProfileViewCont