NSNotificationCenter通知中心

来源:互联网 发布:用visio画网络拓扑图 编辑:程序博客网 时间:2024/04/27 14:32

概述

NSNotificationCenter通知中心,通常用于一对一或者一对多的消息传递,即当一个地方改变时,要求改变其他的一些地方,例如当网络请求回来了新的数据,需要刷新本地信息和本地内存里面的界面时,当这些页面不只一个时就可以考虑使用通知中心


注册一个通知

代码实现

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

[notificationCenter addObserver:self selector:@selector( ) name: object:nil];

发布通知中心事件

代码实现

NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:...];

[[NSNotificationCenter defaultCenter] postNotificationName: object:nil userInfo:dic];

注:dic为传给selector的参数

通知的响应事件

-(void)action:(NSNotification *)sender{

NSDictionary *dic = [sender userInfo];

}

从通知中心移除通知

[[NSNotificationCenter defaultCenter] removeObserver:self name: object:nil];

0 0
原创粉丝点击