HTDelegateProxy

Showing:

Popularity

Downloads/wk

0

GitHub Stars

167

Maintenance

Last Commit

6yrs ago

Contributors

4

Package

Dependencies

0

License

MIT

Categories

Readme

HotelTonight

HTDelegateProxy

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

CocoaPods users:

Add the following line to your Podfile:
pod 'HTDelegateProxy', '~> 0.0.2'

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:

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100