HTDelegateProxy is an NSProxy subclass that allows you to assign multiple delegates to a single source.
Check out the associated blog post at http://engineering.hoteltonight.com/handling-multiple-delegates-in-ios
HTDelegateProxy operates on two simple rules:
This pattern seems to be effective in identifying which messages are informative (hey, something happened) and which messages are more complex interactions (how should I do this?).
Add the following line to your Podfile:
pod 'HTDelegateProxy', '~> 0.0.2'
Add the HTDelegateProxy.m/h files to your project.
Delegates are not retained, so you have to maintain a strong reference to your HTDelegateProxy instance.
For example, you may assign multiple delegates to a UIScrollView by using HTDelegateProxy as follows:
#import "HTDelegateProxy.h" ... @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) HTDelegateProxy *delegateProxy; ...
... self.scrollView = [[UIScrollView alloc] init]; self.delegateProxy = [[HTDelegateProxy alloc] initWithDelegates:@[firstDelegate, secondDelegate]]; self.scrollView.delegate = (id)self.delegateProxy; ...
The HTDelegateProxy class is intentionally immutable in order to enforce the good practice of setting an object's delegate property at the same time that the HTDelegateProxy instance is initialized. The reason for this is that many
setDelegate: implementations (UIScrollView, for example) will call
respondsToSelector: on the delegate in advance, as opposed to when the message is about to be sent (to optimize performance). When this happens, a message will only be sent to instance of HTDelegateProxy if that message is passes
Tweet the authors @jakejennings and @jonsibs, and check out HotelTonight's engineering blog: http://engineering.hoteltonight.com
Also, check out HotelTonight's other iOS open source: