NSNotification

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];
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容