NSNotificationCenter消息机制的介绍

转载自http://www.cnblogs.com/pengyingh/articles/2367374.html


NSNotificationCenter的作用是专门提供程序中不同类之间的消息通讯而设置的。

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

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onFuckme:)   name:strFuckMe object:nil];

参数介绍:

          addObserver: 观察者,即在什么地方接收通知;

        selector: 收到通知后调用何种方法;

        name: 通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。


发送通知:调用观察者处的方法。

<span style="font-size:14px;">[[NSNotificationCenter defaultCenter] postNotificationName:@"mytest" object:searchFriendArray];</span>

参数:

         postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

                 object:传递的参数

注册方法的写法:

- (void) mytest:(NSNotification*) notification
{
   id obj = [notification object];//获取到传递的对象
} 

删除通知:

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


原文地址:https://www.cnblogs.com/AbeDay/p/5026956.html