multiple listeners for delegate iOS

前端 未结 3 1877
抹茶落季
抹茶落季 2021-02-14 10:20

I have a class search bar with a delegate didSelectString. I have an class A that implements the delegate and a class B that implements the delegate.

However only the de

3条回答
  •  日久生厌
    2021-02-14 10:57

    You can easily set up a trampoline object that acts as a delegate multiplexer. The idea is to use a proxy object that will stand-in for an array of delegates. When a method is invoked, it will override forwardInvocation or use *IMP_implementationWithBlock* to pass the message on to each of the delegates in the array.

    Then all you need to do is add methods: attachListener and removeListener (btw: see how this is starting to get similar to notifications?)

    Here's a sample project: https://github.com/aleph7/MultiDelegate

    For more information check out the awesome Objective-C Runtime: https://developer.apple.com/library/mac/documentation/cocoa/reference/objcruntimeref/Reference/reference.html

提交回复
热议问题