前言
最近公司组织两个星期的新人培训,事情安排的满满的,周末都没有。说好的一个星期一更新的博客中断了,让大家久等了,现在培训结束,终于又可以安安静静的做一个程序员了,好开心。。。
一、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] 通知的。
使用步骤
第一步:注册系统监听事件
|
1
2
3
4
5
6
|
//在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];
|
第二步:事件触发后的处理
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/**
* 弹出键盘事件触发处理
*
* @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中解除监听
|
1
2
3
4
5
6
|
/**
*nsnotificationcenter 注意点:每一次在接受者对象中需要delleac把它销毁掉。
*/
-(void)dealloc{
[[nsnotificationcenter defaultcenter] removeobserver:self];
}
|
三、自定义nsnotification
这里我使用的一个实例为:在viewcontroller中定义一个按钮,点击该按钮,同时改变两个自定义view中的内容。
使用步骤
第一步、在viewcontroller中生成一个按钮,两个自定义view
|
1
2
3
4
5
6
7
8
9
10
11
|
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
|
1
2
3
|
-(void)postmsg: (uibutton *)btn{
[[nsnotificationcenter defaultcenter] postnotificationname:notification_message_name object:nil userinfo:@{@"msg":@"jingming1"}];
}
|
第三步、在自定义view中注册监听事件
第四步、处理监听事件
|
1
2
3
4
5
|
-(void)acceptmsg : (nsnotification *)notification{
nslog(@"%@",notification);
nsdictionary *userinfo = notification.userinfo;
_label.text = [userinfo objectforkey:@"msg"];
}
|
第五步、在dealloc中解除监听
|
1
2
3
|
-(void)dealloc{
[[nsnotificationcenter defaultcenter] removeobserver:self];
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 53
-
2025-06-04 102
-
2025-05-27 89
-
2025-05-25 96
-
2025-05-29 83


