iphone ios 消息通信机制NSNotificationCenter

NSNotificationCenter是专门供程序中不同类间的消息通信而设置的,使用起来极为方便,

设置通知,就是说要在什么地方(哪个类)接受通知,一般在初始化中做。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@" test" object:nil];

我仅对以上参数做以说明:addObserver 这个是观察者,就是说 在什么地方接收通知;
 selector 这个是收到通知后,调用何种方法;
 name: 这个是通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
发送通知,就是说此时要调用观察者处的方法。

[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:searchFriendArray];

我仅对以上参数做以说明:
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:传递的参数
发送通知时,默认调用test方法。

- (void) test:(NSNotification*) notification
{
searchFriendArrary = [notification object];//通过这个获取到传递的对象

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章