iOS开发 避免重复执行通知事件的方法

1、创建通知

这个方法需要一个id类型的值接受

@property (nonatomic, weak) id observe;

再创建通知

//Name: 通知的名称

//object:谁发出的通知

//queue: 队列,决定 block 在哪个线程中执行, nil 在发布通知的线程中执行

//usingBlock: 只要监听到通知,就会执行这个 block

_observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"tongzhi" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {

NSLog(@"收到了通知");

}];

该方法有个block,要操作的步骤可以直接写在block里面

2、发送通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];

3、移除通知

- (void)dealloc {
//移除观察者 _observe
[[NSNotificationCenter defaultCenter] removeObserver:_observe];

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

推荐阅读更多精彩内容