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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With