IOS提供了一种“同步的”消息通知机制,观察者只要向消息中心注册,即可接受其他对象发送来的消息,消息的发送者和接受者之间是互相一无所知的,完全解耦。观察者可以有多个,所以消息具有广播的性质,即为观察者模式。
使用此消息机制的步骤:
1、观察者注册消息通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotificationSuccess:) name:@"Notification" object:nil];
2、发送消息通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:nil userInfo:nil];
3、观察者处理消息
- (void) getNotificationSuccess: (NSNotification*)pNotification
{
}
4、观察者注销,移除消息观察者
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"Notification_GetUserProfileSuccess" object:nil];