IOS 通知

现在发通知的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: 通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS 提供了一种 “同步的” 消息通知机制NSNotificationCenter,观察者只要向消息中心注册, ...
    MasterChen阅读 6,655评论 4 16
  • 要了解通知、KVO、代理的区别,合理利用。 [A]通知是什么: 通知(NSNotification)是iOS的一种...
    YH的练手记阅读 1,235评论 0 0
  • 如果在一个类中想要执行另一个类中的方法可以使用通知1.创建一个通知对象:使用notificationWithNam...
    wangjianjun0730阅读 4,064评论 0 0
  • NSNotificationCenter 在这里第一步和第二步的顺序可以互换,一般楼主我喜欢先在需要发送消息的页面...
    Joker_King阅读 5,914评论 1 1
  • 前言 今年3月阿里云一个虚拟主机的活动, 13块钱买了一个域名和一年的虚拟主机,然后发现这是个坑,不备案的话这个域...
    logan丶阅读 11,535评论 12 5