美文网首页ios 进阶
iOS 里的通知中心 NSNotificationCenter

iOS 里的通知中心 NSNotificationCenter

作者: K__M | 来源:发表于2016-03-22 17:34 被阅读181次

1、注册通知:addObserver:selector:name:object,并实现触发通知后要实现的操作

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changePersonState) name:@"PersonState" object:nil];

// 通知方法

- (void)changePersonState {

[self.tableView reloadData];

}

 2、发送通知:postNotificationName:object(就在触发通知的方法里写)

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

3、移除通知:removeObserver:和removeObserver:name:object:

// 移除单个通知

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"JPUSHNOTIFICATION" object:self];

// 移除当前所有通知

[[NSNotificationCenter defaultCenter]removeObserver:self];

相关文章

网友评论

  • feng_dev:移除所有通知 是 移除当前 控制器的所有通知 ,还是 项目中所有的 通知啊?

本文标题:iOS 里的通知中心 NSNotificationCenter

本文链接:https://www.haomeiwen.com/subject/dqrllttx.html