HTDelegateProxy 1.0.1

HTDelegateProxy 1.0.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Unclaimed.



  • By
  • Jacob Jennings

Overview

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:

  1. Messages with a void return type are sent to all target delegates that reponds to the selector
  2. Messages with non-void return types are sent only to the first delegate in the list that responds to the selector.

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?).

Installation

Everyone else:

Add the HTDelegateProxy.m/h files to your project.

Usage

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:

In your interface:

#import "HTDelegateProxy.h"
...
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) HTDelegateProxy *delegateProxy;
...

In your class:

...
self.scrollView = [[UIScrollView alloc] init];
self.delegateProxy = [[HTDelegateProxy alloc] initWithDelegates:@[firstDelegate, secondDelegate]];
self.scrollView.delegate = (id)self.delegateProxy;
...

Discussion

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 respondesToSelector:.

Use it? Love/hate it?

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: