通知的添加和移除通过block实现

来源:互联网 发布:易建联nba首秀数据 编辑:程序博客网 时间:2024/06/05 18:41

//监听通知

 __blockid notification = [[NSNotificationCenterdefaultCenter]addObserverForName:NOTI_WEALTH_SUCCESS

 object:nil  queue:[NSOperationQueuemainQueueusingBlock:^(NSNotification *note)

 {

  CKModelWealth *wealth = note.object;

//执行方法

。。。。。。。

//方法执行完后,移除监听事件   

[[NSNotificationCenterdefaultCenter]removeObserver:notification];

 }];



监听后必须移除,不然通知个数太多的话会很影响运行速度。一般是 添加监听  ---》 发送通知 ----》接收通知  ----》移除监听

#if NS_BLOCKS_AVAILABLE

- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block NS_AVAILABLE(10_6,4_0);

    // The return value is retained by the system, and should be held onto by the caller in

    // order to remove the observer with removeObserver: later, to stop observation.

#endif



//发送通知

[[NSNotificationCenterdefaultCenter]postNotificationName:NOTI_WEALTH_SUCCESSobject:wealth];