IOS开发基础篇--NSNotificationCenter使用小结
2015-08-08 23:15
489 查看
前言
最近公司组织两个星期的新人培训,事情安排的满满的,周末都没有。说好的一个星期一更新的博客中断了,让大家久等了,现在培训结束,终于又可以安安静静的做一个程序员了,好开心。。。一、NSNotification和Delegate的联系和区别
众所周知,IOS中经常会使用到NSNotification和delegate来进行一些类之间的消息传递。言归正传,这两种有什么区别呢?NSNotification就是IOS提供的一个消息中心,由一个全局的defaultNotification管理应用中的消息机制。通过公开的API可以看出,这里面使用了是一个观察者,通过注册addObserver和解除注册removeObserver来实现消息传递。苹果文档特别提出,在类析构的时候,要记得把removeObserver,不然就会引发崩溃,所以NSNotifcation的使用是没有retain+1的,NSNotification是一对多的。
至于Delegate,很简单,就是通过增加一个指针,然后把需要调用的函数通过delegate传递到其他类中,来得很直截了当。不需要通过广播的形式去实现,但是,delegate的形式只能是一对一,不能实现一对多。
在什么情况下使用Delegate和NSNotifiation呢?
从效率上看Delegate是一个很轻量级的,相对delegate,NSNotification却是一个很重量级的,效率上delegate明显要比Noticication高。一般情况我们会这样使用。
场景一:
A拥有B,然后B中的一些操作需要回调到A中,这时候就简单的通过delegate回调到A。因为B是A创建的,B可以很直接的把delegate赋值A。
场景二:
A和B是两个不相干的关系,A不知道B,B也不知道A,那么这时候如果通过delegate就没办法做到,会相对复杂。所以可以通过NSNotifcation去做一些消息传递。
所以使用delegate的情况是两者有直接的关系,至于一方知道另一方的存在。而NSNotifcation一般是大家不知道对方的存在,一般是使用跨模块的时候使用。在使用的时候,使用delegate可能需要多写一些delegate去实现,代码量比较多。NSNotication只要定义相关的NotificationName就可以很方便的沟通。两者各有所长。
二、监听系统自带的NSNotification
系统里定义了许多的 XxxNotification 名称,其实只要 Cmd+Shift+O 打开 Open Quickly,输入 NSNotification 或者 UINotification 可以看到许多以 Notification 结尾的变量定义,由变量名称也能理解在什么时候会激发什么事件,一般都是向 [NSNotificationCenter defaultCenter] 通知的。使用步骤
第一步:注册系统监听事件
[code] //在NSNotificationCenter中注册键盘弹出事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardUpEvent:) name:UIKeyboardDidShowNotification object:nil]; //在NSNotificationCenter中注册键盘隐藏事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDownEvent:) name:UIKeyboardDidHideNotification object:nil]; //在NSNotificationCenter中注册程序从后台唤醒事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
第二步:事件触发后的处理
[code]/** * 弹出键盘事件触发处理 * * @param notification */ -(void)keyboardUpEvent : (NSNotification *)notification{ //NSLog(@"键盘弹出事件触发==%@",notification); NSLog(@"键盘弹出事件触发"); } /** * 键盘隐藏事件触发处理 * * @param notification */ -(void)keyboardDownEvent : (NSNotification *)notification{ //NSLog(@"键盘隐藏事件触发==%@",notification); NSLog(@"键盘隐藏事件触发"); } /** * 程序从后台唤醒触发处理 * * @param notification */ -(void)becomeActive: (NSNotification *)notification{ NSLog(@"程序从后台唤醒触发处理"); }
第三步、在dealloc中解除监听
[code]/** *NSNotificationCenter 注意点:每一次在接受者对象中需要delleac把它销毁掉。 */ -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; }
三、自定义NSNotification
这里我使用的一个实例为:在ViewController中定义一个按钮,点击该按钮,同时改变两个自定义View中的内容。使用步骤
第一步、在ViewController中生成一个按钮,两个自定义View
[code] UIButton *postMsgBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 40)]; [postMsgBtn setTitle:@"发送消息" forState:UIControlStateNormal]; postMsgBtn.backgroundColor = [UIColor grayColor]; [postMsgBtn addTarget:self action:@selector(postMsg:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:postMsgBtn]; MyView *view = [[MyView alloc] initWithFrame:CGRectMake(50, 250, 100, 50)]; [self.view addSubview:view]; MyView *view2 = [[MyView alloc] initWithFrame:CGRectMake(50, 320, 100, 50)]; [self.view addSubview:view2];
第二步、点击按钮,发送Notification
[code]-(void)postMsg: (UIButton *)btn{ [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_MESSAGE_NAME object:nil userInfo:@{@"msg":@"jingming1"}]; }
第三步、在自定义View中注册监听事件
[code][[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:NOTIFICATION_MESSAGE_NAME object:nil];
第四步、处理监听事件
[code]-(void)acceptMsg : (NSNotification *)notification{ NSLog(@"%@",notification); NSDictionary *userInfo = notification.userInfo; _label.text = [userInfo objectForKey:@"msg"]; }
第五步、在dealloc中解除监听
[code]-(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; }
四、参考文献
本来想自己总结文字写的,鉴于自己时间有限,而且网上有些博客总结也挺不错的,跟自己的想法很符合。所以有些就把别人的知识成果搬过来了,在次声明一下。http://m.blog.csdn.net/blog/hzhenx1989/20217387
http://www.cnblogs.com/wenxp2006/articles/2499330.html
五、博主的话
以前看过很多别人的博客,学到不少东西。现在准备自己也开始写写博客,希望能够帮到一些人。联系方式:
微博:新浪微博
博客:http://blog.csdn.net/yixiangboy
github:https://github.com/yixiangboy
相关文章推荐
- IOS开发之NSString的使用大全
- iOS Core Animation详解(五)CATransition
- 运行时Runtime 小结
- iOS开发概述 - 15.核心动画概述(Core Animation)
- iOS开发概述 - 14.CALayer隐式动画
- iOS程序启动的完整过程
- iOS开发拓展篇—静态库
- iOS 调试用Log()与随机颜色设置
- [IOS]IOS绘画概念
- iOS-线程之GCD---之GCD的几种常用常用的方法
- iOS-线程之GCD方式---之同步异步和串行队列并行队列之间的关系
- iOS第三方Api及常用框架总结
- HDU 1017 A Mathematical Curiosity
- 开源中国IOSclient
- iOS 读书笔记-国际化
- iOS NSOperation 封装 通知实现界面更新
- iOS NSOperation 异步加载图片 封装NSOperation 代理更新
- 首次使用pod遇到问题后在stackoverflow寻找的解决方案
- iOS ARC 下的单例模式
- iOS 8 地图授权