iOS 提供了一种 "同步的" 消息通知机制
观察者只要向消息中心注册, 即可接受其他对象发送来的消息
使用消息机制的步骤:
a.观察者注册消息通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserProfileSuccess:) name:@"Notification_GetUserProfileSuccess" object:nil];
- notificationObserver 观察者 : self
- notificationSelector 处理消息的方法名: getUserProfileSuccess
- notificationName 消息通知的名字: Notification_GetUserProfileSuccess
- notificationSender 消息发送者 : 表示接收哪个发送者的通知,如果第四个参数为nil,接收所有发送者的通知
b. 发送消息通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification_GetUserProfileSuccess" object:userProfile userInfo:nil];
- notificationName 消息通知的名字: Notification_GetUserProfileSuccess
- notificationSender 消息发送者: userProfile
c. 观察者处理消息
- (void) getUserProfileSuccess: (NSNotification*) aNotification
d.观察者注销,移除消息观察者
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}