iOS 通知中心

通知相比于代理,通知是多对多的关系,实现通知需要下面三个步骤,注册了一个要用的通知,实现通知的方法,最后再需要调用这个方法的地方post一下这个通知,那么这个方法便会调用了

 

注册通知:即要在什么地方接受消息

[[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector (test:) name:@"postData" object:nil];
参数介绍:
addObserver:观察者,即在什么地方接收通知;
selector:收到通知后调用何种方法,即回调函数;
name:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

  

 通知方法:

    -(void)test:(NSNotification *)notification{
        id text = notification.object;
        UILabel *label = (UILabel*)[self.viewviewWithTag:102];
        label.text = text;
    }

  

通知的调用:

[[NSNotificationCenterdefaultCenter] postNotificationName:@"postData" object:self._textField.text];//self._textField.text为要传的参
    
如果在post 通知时不需要传参数则:
[[NSNotificationCenter defaultCenter] postNotificationName:@"postData" object:nil];

  

posted @ 2016-01-15 11:25  欲眠  阅读(134)  评论(0编辑  收藏  举报