博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发-消息通知机制(NSNotification和NSNotificationCenter)
阅读量:7099 次
发布时间:2019-06-28

本文共 2602 字,大约阅读时间需要 8 分钟。

iOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改变要映射到页面A,页面C的值改变也需要映射到页面A,那么就需要需要两个委托解决问题。NSNotificaiton则是一对多注册一个通知,之后回调很容易解决以上的问题。

基础概念 

iOS消息通知机制算是同步的,观察者只要向消息中心注册, 即可接受其他对象发送来的消息,消息发送者和消息接受者两者可以互相一无所知,完全解耦。
这种消息通知机制可以应用于任意时间和任何对象,观察者可以有多个,所以消息具有广播的性质,只是需要注意的是,观察者向消息中心注册以后,在不需要接受消息时需要向消息中心注销,属于典型的观察者模式。

消息通知中重要的两个类:

(1)NSNotificationCenter: 实现NSNotificationCenter的原理是一个观察者模式,获得NSNotificationCenter的方法只有一种,那就是[NSNotificationCenter defaultCenter] ,通过调用静态方法defaultCenter就可以获取这个通知中心的对象了。NSNotificationCenter是一个单例模式,而这个通知中心的对象会一直存在于一个应用的生命周期。

  (2) NSNotification: 这是消息携带的载体,通过它,可以把消息内容传递给观察者。

实战演练

1.通过NSNotificationCenter注册通知NSNotification,viewDidLoad中代码如下:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationFirst:) name:@"First" object:nil];        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSecond:) name:@"Second" object:nil];

第一个参数是观察者为本身,第二个参数表示消息回调的方法,第三个消息通知的名字,第四个为nil表示表示接受所有发送者的消息~

回调方法:

-(void)notificationFirst:(NSNotification *)notification{    NSString  *name=[notification name];    NSString  *object=[notification object];    NSLog(@"名称:%@----对象:%@",name,object);}-(void)notificationSecond:(NSNotification *)notification{    NSString  *name=[notification name];    NSString  *object=[notification object];    NSDictionary  *dict=[notification userInfo];    NSLog(@"名称:%@----对象:%@",name,object);    NSLog(@"获取的值:%@",[dict objectForKey:@"key"]);}

2.消息传递给观察者:

[[NSNotificationCenter defaultCenter] postNotificationName:@"First" object:@"博客园-Fly_Elephant"];        NSDictionary  *dict=[[NSDictionary alloc]initWithObjects:@[@"keso"] forKeys:@[@"key"]];        [[NSNotificationCenter defaultCenter] postNotificationName:@"Second" object:@"http://www.cnblogs.com/xiaofeixiang" userInfo:dict];

3.页面跳转:

 

-(void)pushController:(UIButton *)sender{    ViewController *customController=[[ViewController alloc]init];    [self.navigationController pushViewController:customController animated:YES];}

4.销毁观察者

-(void)dealloc{    NSLog(@"观察者销毁了");    [[NSNotificationCenter defaultCenter] removeObserver:self];}

 也可以通过name单个删除:

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

5.运行结果

2015-04-26 15:08:25.900 CustoAlterView[2169:148380] 观察者销毁了2015-04-26 15:08:29.222 CustoAlterView[2169:148380] 名称:First----对象:博客园-Fly_Elephant2015-04-26 15:08:29.222 CustoAlterView[2169:148380] 名称:Second----对象:http://www.cnblogs.com/xiaofeixiang2015-04-26 15:08:29.223 CustoAlterView[2169:148380] 获取的值:keso

转载于:https://www.cnblogs.com/xiaofeixiang/p/4457792.html

你可能感兴趣的文章
mahout0.7 示例运行纪实
查看>>
centos7快速部署ceph
查看>>
git安装和使用案例
查看>>
cisco路由器进入rommon模式
查看>>
awk Tips
查看>>
继承抽象类
查看>>
(摘)Excel 2007查询操作中的函数应用
查看>>
使用SCOM资源工具包来协助MP开发及调试
查看>>
从Linux终端管理进程:10个你必须知道的命令
查看>>
centos7 命令补全
查看>>
JAVA递归中的垃圾回收
查看>>
PyTables Windows平台安装说明
查看>>
Linux crontab定时执行任务 命令格式与详细例子
查看>>
IDC: 2018年智能家居将打破设备孤岛瓶颈
查看>>
Redis的三种启动方式
查看>>
python多线程之线程锁三(同一时间允许多个线程)
查看>>
PinPoint分布式全链路监控
查看>>
【Flume】HDFSSink配置参数说明
查看>>
面向对象学习
查看>>
CentOS7修改主机名
查看>>