ios-通知简单示例
2016-04-05 00:36
435 查看
通知是一种一对多的信息广播机制,一个应用程序同时只能有一个NSNotificationCenter(通知中心)对象,用来添加通知观察者或者说监听者,以及发送通知.
用的地方是:不同控制器的传值回调.delegate和block也属于一种信息传递机制,但这两种都是一对一的,每次执行的方法都不一样,而通知是一对多,只要有地方触发通知,执行的是同一个方法。
注意点:
创建通知中心对象 添加'观察者' observer, 确定通知名字 name,确定监听对象object ,如果object 设置为 nil 则表示监听所有发送名字为 name 的消息. 以及监听到通知(收到通知后)发生的动作 @selector(动作)
让消息post 发送者发送通知 名字为在观察者哪里注册的通知名字 name,是字符串类型的.
a. 最重要的第一点是发布通知的方法中一个是最后的参数userInfo,字典类型,可以将数值传递到@selector(动作) 动作中,动作的参数为NSNotification *notification. notification.userInfo[@"key值"],里面存储了你要传递的数据.
最后要在创建观察者的控制器里面dealloc方法中移除通知中心.
下面看示例演示通知在不同控制器中的运作:
上图是两个控制器,分别绑定不同的类.
第一个控制器ViewController.m文件
在viewDidLoad添加观察者,以及观察者收到通知后触发的方法.设置消息名字.监听对象nil(所有名字为"show"的通知)
由于上述方法是弹框的方法所以应该是谁调用这个方法,在谁的控制器里面弹框.所以利用了下面的方法(PBBViewController里面利用userInfo字典将PBBViewcontroller本身传递进来,让self presentViewController 转换成传递进来的PBBViewcontroller).
第二个控制的.m文件
注意点:postNotificationName此方法中将,需要的控制器传递给ViewController了,以及弹框的message也改成了传递进去的 @"这是哈哈"
用的地方是:不同控制器的传值回调.delegate和block也属于一种信息传递机制,但这两种都是一对一的,每次执行的方法都不一样,而通知是一对多,只要有地方触发通知,执行的是同一个方法。
注意点:
创建通知中心对象 添加'观察者' observer, 确定通知名字 name,确定监听对象object ,如果object 设置为 nil 则表示监听所有发送名字为 name 的消息. 以及监听到通知(收到通知后)发生的动作 @selector(动作)
让消息post 发送者发送通知 名字为在观察者哪里注册的通知名字 name,是字符串类型的.
a. 最重要的第一点是发布通知的方法中一个是最后的参数userInfo,字典类型,可以将数值传递到@selector(动作) 动作中,动作的参数为NSNotification *notification. notification.userInfo[@"key值"],里面存储了你要传递的数据.
最后要在创建观察者的控制器里面dealloc方法中移除通知中心.
下面看示例演示通知在不同控制器中的运作:
上图是两个控制器,分别绑定不同的类.
第一个控制器ViewController.m文件
// // ViewController.m // kvo01 // // Created by 裴波波 on 16/4/4. // Copyright © 2016年 裴波波. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMessage:) name:@"show" object:nil]; }
在viewDidLoad添加观察者,以及观察者收到通知后触发的方法.设置消息名字.监听对象nil(所有名字为"show"的通知)
//控制器发送名字为 @"show" 的通知的时候都会执行该方法 -(void)showMessage:(NSNotification *)notification { //弹框 UIAlertController * alertVc = [UIAlertController alertControllerWithTitle:@"操作提示" message:@"这是通知中心" preferredStyle:UIAlertControllerStyleAlert]; //利用userInfo是字典,将需要的数据传递进来,一个是字符串 NSString * str = notification.userInfo[@"haha"]; UIAlertAction * act = [UIAlertAction actionWithTitle:str style:UIAlertActionStyleDefault handler:nil]; [alertVc addAction:act]; //另一个是将触发这个方法的控制器(self)传递进来--->谁发送的 @"show" 通知 触发的这个方法--弹框,就由哪个Vc来弹出这个弹框,而不是用self presentViewController ---这样不对. [notification.userInfo[@"vc"] presentViewController:alertVc animated:YES completion:nil]; }
由于上述方法是弹框的方法所以应该是谁调用这个方法,在谁的控制器里面弹框.所以利用了下面的方法(PBBViewController里面利用userInfo字典将PBBViewcontroller本身传递进来,让self presentViewController 转换成传递进来的PBBViewcontroller).
//移除通知一般在dealloc中 -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:[ViewController class] name:@"show" object:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
第二个控制的.m文件
// // PBBViewController.m // kvo01 // // Created by 裴波波 on 16/4/4. // Copyright © 2016年 裴波波. All rights reserved. // #import "PBBViewController.h" @interface PBBViewController () @end @implementation PBBViewController - (IBAction)clickSendMessageButton:(id)sender { //获得通知中心对象,发送通知,通知名为在ViewController中注册的通知名 @"show" ,ViewController为观察者,接收通知.接收到通知后出发对应的 @selector(方法) 方法.由于出发的 '方法' 是弹框操作,需要 present 一个controller,而且是谁发送的这个通知,在谁的controller中弹出alertViewController.---通过userInfo字典类型,将对象包装在内部,回传给通知观察者.将调用 present... controller的对象改为传递过去的控制器对象 下面的 @"vc" : (对应的) self ,还可以将弹框的message 改为 @"这是哈哈" [[NSNotificationCenter defaultCenter] postNotificationName:@"show" object:nil userInfo:@{@"vc":self,@"haha":@"这是哈哈"}]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
注意点:postNotificationName此方法中将,需要的控制器传递给ViewController了,以及弹框的message也改成了传递进去的 @"这是哈哈"
能用kvo 以及 block 回调 来传递信息,就不用delegate来传递信息.代理比较繁琐.
相关文章推荐
- OC阅读笔记十二:ARC(上)
- iOS之正则表达式
- 绘制长度不定可自动换行的标签
- 提高iOS开发效率的第三方框架等--不断更新中。。。
- [10秒学会] - iOS录制屏幕 ReplayKit
- 【译】使用Apteligent追踪iOS应用bug
- iOS Runtime机制和使用
- KeyMob移动广告聚合平台专注于IOS、安卓手机广告
- iOS- 响应者链, runtime, runloop
- [IOS] 使用属性列表实现数据持久化
- IOS 简单动画 首尾式动画
- iOS开发者证书-详解/生成/使用,iOS CA认证
- nagios插件之监控udp端口状态
- iOS打电话、发短信
- iOS序列桢动画创建方式
- iOS开发中view controller设置问题
- ios AES、DES、MD5使用
- ios 添加到购物车的动画
- iOS开发-URLSession使用
- hadoop 集群部署ganglia 监控服务与nagios 报警服务