IOS Foundation库相关知识

IOS 通知

2017-08-19  本文已影响1人  小暖风

现在发通知的viewController中

NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:tf.text,@"textField",nil];

[[NSNotificationCente rdefaultCenter] postNotificationName:@"name" object:self userInfo:dict];

然后在要显示的viewController中

注册通知:

[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(done:) name:@"name" object:nil];

添加观察者之后,一定要在dealloc里面记得移除。

显示:

NSDictionary* dict =[notification userInfo];

label.text= [dict objectForKey:@"textField"];

通知 NSNotificationCenter

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

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

参数:

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

object:传递的参数

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

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

- (void) mytest:(NSNotification*) notification

{

id obj = [notification object];//获取到传递的对象

//NSDictionary* dict =[notification userInfo];

//label.text= [dict objectForKey:@"textField"];

}

参数介绍:

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

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

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

上一篇下一篇

猜你喜欢

热点阅读