ios NSNotificationCenter消息通讯机制

作用: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];
    });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值