组播委托 - 多个类从iOS中的UI控件接收通知 [英] Multicast delegates - multiple classes receiving notifications from UI controls in iOS

查看:248
本文介绍了组播委托 - 多个类从iOS中的UI控件接收通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解UI控件,例如UITextField通过其委托通知客户端交互/事件,这被定义为支持所需协议的类。

I understand that UI controls such as UITextField notify of client interactions / events via their delegate, which is defined as a class that supports the required protocol.

我有经常发现自己想要在多个类中接收UI事件的通知,所以想要支持多播。例如,为UI控件指定多个委托。我很确定没有支持这一点的iOS框架功能。我想知道有没有人提出一个体面的解决方案这个问题?

I have often found myself wanting to receive notifications of UI event in more than one class, so would want to support multicasting. For example, specifying more than one delegate for a UI control. I am pretty sure that there is no iOS framework feature that supports this. I was wondering if anyone had come up with a decent solution to this problem?

推荐答案

有一个可可功能,让你建立多播代理相对容易 - 这是邮件转发 系统内置到框架中。

There is a cocoa feature that lets you build multicast delegates with relative ease - it's the Message Forwarding system built into the framework.

创建一个覆盖 forwardInvocation:的类,并返回您的对象的实例,而不是代理。这有时被称为蹦床对象。现在你的 forwardInvocation:实现中的逻辑可以决定哪些真实对象应该接收消息,并将调用转发给一个或多个。

Make a class that overrides forwardInvocation:, and return an instance of your object instead of a delegate. This is what is sometimes called a "Trampoline object". The logic inside your forwardInvocation: implementation can now decide which "real" objects should receive the message, and forward the invocation to one or more of them.

作为对此接受答案的更新,我在此创建了自己的组播委托实现:

As an update to this accepted answer, I created my own multicasting delegate implementation here:

http://www.scottlogic.co.uk/blog/colin/2012/ 11 / a-multicast-delegate-pattern-for-ios-controls /

这篇关于组播委托 - 多个类从iOS中的UI控件接收通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆