IOS开发(45)之delegate和Notification的区别
来源:互联网 发布:淘宝日服苹果账号注册 编辑:程序博客网 时间:2024/04/28 23:47
delegate针对one-to-one关系,并且reciever可以返回值给sender;
notification 可以针对one-to-one/many/none,reciever无法返回值给sender;
所以,delegate用于sender希望接受到reciever的某个功能反馈值,notification用于通知多个object某个事件。
delegate主动,notification被动
1.
Delegate:
消息的发送者(sender)告知接收者(receiver)某个事件将要发生,delegate同意然后发送者响应事件,delegate机制使得接收者可以改变发送者的行为。通常发送者和接收者的关系是直接的一对多的关系。
Notification:
消息的发送者告知接收者事件已经发生或者将要发送,仅此而已,接收者并不能反过来影响发送者的行为。通常发送者和接收者的关系是间接的多对多关系。
2.
很早stepwise上有一篇很经典的文章抓没讲delegate和notificationde的区别的,不过很遗憾现在stepwise下线了。
简单说,
1. 效率肯定是delegate比nsnotification高。
2. delegate方法比notification更加直接,最典型的特征是,delegate方法往往需要关注返回值,也就是delegate方法的结果。比如-windowShouldClose:,需要关心返回的是yes还是no。所以delegate方法往往包含should这个很传神的词。也就是好比你做我的delegate,我会问你我想关闭窗口你愿意吗?你需要给我一个答案,我根据你的答案来决定如何做下一步。相反的,notification最大的特色就是不关心接受者的态度,我只管把通告放出来,你接受不接受就是你的事情,同时我也不关心结果。所以notification往往用did这个词汇,比如NSWindowDidResizeNotification,那么nswindow对象放出这个notification后就什么都不管了也不会等待接受者的反应。
- IOS开发(45)之delegate和Notification的区别
- iOS开发:Delegate、Notification和KVO的联系与区别
- ios delegate和notification的区别
- iOS----delegate和Notification的区别
- delegate和Notification的区别
- delegate和Notification的区别
- delegate和Notification的区别
- delegate和notification的区别
- delegate和Notification的区别
- delegate和Notification的区别
- delegate和notification的区别
- delegate和notification的区别
- IOS中delegate、notification和KVO的区别和介绍
- ios开发之何时用delegate,何时用notification?
- ios开发之何时用delegate,何时用notification?
- delegate 与 notification 的区别
- Delegate,notification,KVO的区别
- iOS通讯模式(KVO、Notification、Delegate、Block、Target-Action的区别)
- 直接拿来用!最火的Android开源项目(一)
- linux下静态库与动态库的生成与调用
- matlab里面的求协方差函数
- 第九周上机任务项目2-Time类
- ELF动态解析符号过程(修订版)
- IOS开发(45)之delegate和Notification的区别
- Boost.Interprocess使用手册翻译之四:在进程间共享内存 (Sharing memory between processes)
- 用INNO SETUP做安装程序时,如何自动注册一个自己开发的DLL文件??
- ICE Manual(Documentation for Ice 3.5)---The Ice Run Time in Detail(ICE Stats)
- 关于用Inno Setup打包的问题 Flags: uninsneveruninstall
- Boost.Interprocess使用手册翻译之五:独立于映射地址的指针:offset_pt (Mapping Address Independent Pointer: offset_ptr)
- Orcale权限、角色
- Boost.Interprocess使用手册翻译之六:同步机制(Synchronization mechanisms)
- Android中从SD卡中/拍照选择图片并进行剪裁的方法