Communicate between objects
来源:互联网 发布:手机上搞怪的软件 编辑:程序博客网 时间:2024/05/16 17:34
By Matthijs
When you have two objects A and B, says two view controllers, that you want to make talk to each other, you can choose from the following options:
NSNotifiationCenter.This is anonymous one-to-many communication. Objects A posts a notification to the NSNotificationCenter, which then distributes it to any other objects listening for that notification, including Object B. A and B do not have to know anything about each other, so this is a very loose coupling.
KOV (Key-Value Observing).One object observes the properties of another. This is a very tight coupling, because Objects B is now peeking directly into Object A. The advantage of KVO is that Object A doesn't have to be aware of this at all, and therefore does not need to send out any notifications -- the KVO mechanism takes care of this behind the scenes.
Direct Pointers. Object A has a pointer to Object B and directly sends it messages when something of interest happens. This is the tightest coupling possible because A and B cannot function without each other. In the case of view controllers you generally want to avoid this.
Delegates. Object B is a delegate of Object A. In this scenario, Object A does not know anything about Object B. It just knows that some object performs the role of its delegate and it will happily send messages to that delegate, but it doesn't know -- or care -- that this is Object B. The delegate pattern is often the preferred way to communicate between view controllers, but it takes some work to set up.
Blocks. Essentially the same approach as delegates, except that Object B now gives Object A one or more blocks(closures) to be executed when certain events take place. There is no formal delegates protocol and the only thing that Object A sees of Object B is the blocks it is given.
- Communicate between objects
- Using Delegates to communicate between Windows Forms
- communicate
- Passing Objects Between Android Activities
- Using LocalConnection to communicate between two Flex applications
- Difference Between Crystal Reports and Business Objects
- The relationship between Objects and Closures
- Item7 Distinguish between () and {} when creating objects
- How to: Use Named Pipes to Communicate Between Processes over a Network
- [MapReduce]students on forums that communicate a lot between themselves
- cast between Objective-C and Core Foundation-style objects
- Number of days between two Date objects (Ruby)
- Image stabilization in vertebrates depends on contrast between objects
- wxWidgets communicate
- communicate patterns
- How to copy all view private files and Derived objects between views
- How to copy all view private files and Derived objects between views
- confilicts between permissions on a store procedure and the objects it referers to
- 乱码问题解决方法
- 字符串函数 值的互换
- 正则表达式详解
- 用js实现复选框的反选和全选和全不选
- hdu 2089 不要62
- Communicate between objects
- JSTL标准标签库
- EI收录中国期刊目录 各个版本的含义及收录例子-12年初版
- 11个强大的Visual Studio调试小技巧
- 标签实现相册的不同显示
- GLUI配置
- 实现网页上的坐标查询
- linux线程模型
- Scrapy爬虫入门