ios NSNotificationCenter消息通讯机制
来源:互联网 发布:turtlebot编程 编辑:程序博客网 时间:2024/06/06 12:43
作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的.
注册通知:即要在什么地方接受消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mytest:) name:@" mytest"object:nil];
**参数介绍:
addObserver: 观察者,即在什么地方接收通知;
selector: 收到通知后调用何种方法;
name: 通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
发送通知:调用观察者处的方法。**
发送通知:调用观察者处的方法。
[[NSNotificationCenter defaultCenter] postNotificationName:@"mytest" object:searchFriendArray];
参数: postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。 object:传递的参数
注册方法的写法:- (void) mytest:(NSNotification*) notification{ id obj = [notification object];//获取到传递的对象}
附:注册键盘升启关闭消息//键盘升起 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];//键盘降下[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
看一个程序,里面viewDidLoad中有 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@"saveMessage" object:nil]; [center addObserver:self selector:@selector(saveMessage) name:@"saveMessage" object:nil];不明白为什么先去掉注册者,然后又添加?不是同一个observer吗?消息傳送机构:举例说明
在有需要的类中,发送消息//发送消息出去,这里的对象是一个数组:saveImageArray[[NSNotificationCenter defaultCenter] postNotificationName:@"postData" object:saveImageArray];所有亲朋好友给我给包(发送消息),,,
//注册一个observer来响应消息的传送[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PostImage:)//接收消息方法 name:@"postData"//消息识别名称 object:nil];
//实现方法 -(void)PostImage:(NSArray *)array{ //接收传送过来的消息} 我把红包都收起来,(接收消息)
//移除observer [[NSNotificationCenter defaultCenter] removeObserver:self name:@"postData" object:nil];红包都收完了,哈哈,亲朋好友回家咯。。
在我们开发中,我们经常可以看到这样的代码:- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"test" object:nil];}- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@"test" object:nil];}就是在页面出现的时候注册通知,页面消失时移除通知。你这边可要注意了,一定要成双成对出现,如果你只在viewWillAppear 中 addObserver没有在viewWillDisappear 中 removeObserver那么当消息发生的时候,你的方法会被调用多次,这点必须牢记在心。
NSNotificationCenter消息的接受线程是基于发送消息的线程的。也就是同步的,因此,有时候,你发送的消息可能不在主线程,而大家都知道操作UI必须在主线程,不然会出现不响应的情况。所以,在你收到消息通知的时候,注意选择你要执行的线程。下面看个示例代码
/接受消息通知的回调- (void)test{ if ([[NSThread currentThread] isMainThread]) { NSLog(@"main"); } else { NSLog(@"not main"); } dispatch_async(dispatch_get_main_queue(), ^{ //do your UI });}//发送消息的线程- (void)sendNotification{ dispatch_queue_t defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(defaultQueue, ^{ [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil]; });}
0 0
- ios NSNotificationCenter消息通讯机制
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- 【IOS】消息通信机制NSNotificationCenter
- ios NSNotificationCenter消息通信机制
- IOS学习--NSNotificationCenter消息机制
- Python代码优化及技巧笔记(一)
- mysql(一)
- C语言极简入门-VC6.0极速安装-2
- Swift变量和常量
- vi/vim基本使用方法
- ios NSNotificationCenter消息通讯机制
- 分布式系统中不可靠的TCP
- 13.5.2.javascript内存与性能与13.6事件模拟
- git命令大全(非常齐全)
- 最小生成树Minimum Spanning Tree
- Lambda, filter, reduce and map[pthon]
- 门电路逻辑符号大全(三态门,同或门,异或门,或非门,与或非门, 传输门,全加器,半加器等)
- java 泛型原理
- windows命令行下查看进程号