This repository has been archived by the owner on Dec 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
A2DynamicDelegate.h
194 lines (150 loc) · 6.92 KB
/
A2DynamicDelegate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// A2DynamicDelegate.h
// A2DynamicDelegate
//
// Created by Alexsander Akers on 11/26/11.
// Copyright (c) 2011 Pandamonia LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
/** A2DynamicDelegate implements a class's delegate,
data source, or other delegated protocol by associating
protocol methods with a block implementations.
- (IBAction) annoyUser
{
// Create an alert view
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: @"Hello World!"
message: @"This alert's delegate is implemented using blocks. That's so cool!"
delegate: nil
cancelButtonTitle: @"Meh."
otherButtonTitles: @"Woo!", nil];
// Get the dynamic delegate
A2DynamicDelegate *dd = alertView.dynamicDelegate;
// Implement -alertViewShouldEnableFirstOtherButton:
[dd implementMethod: @selector(alertViewShouldEnableFirstOtherButton:) withBlock: ^(UIAlertView *alertView) {
NSLog(@"Message: %@", alertView.message);
return YES;
}];
// Implement -alertView:willDismissWithButtonIndex:
[dd implementMethod: @selector(alertView:willDismissWithButtonIndex:) withBlock: ^(UIAlertView *alertView, NSInteger buttonIndex) {
NSLog(@"You pushed button #%d (%@)", buttonIndex, [alertView buttonTitleAtIndex: buttonIndex]);
}];
// Set the delegate
alertView.delegate = dd;
[alertView show];
[alertView release];
}
A2DynamicDelegate is designed to be 'plug and play'. It just works. Pretty neat, huh?
@warning An A2DynamicDelegate cannot simply be allocated. Calling one of the
A2DynamicDelegate methods on NSObject, -dynamicDataSource, -dynamicDelegate,
or -dynamicDelegateForProtocol: allows us to store the dynamic delegate as
an associated object of the delegating object. This not only allows us to
later retrieve it, but it also creates a strong relationship. Since delegates
are usually weak references on the part of the delegating object, a dynamic
delegate would be deallocated immediately after its declaring scope ends.
*/
@interface A2DynamicDelegate : NSObject {
id _delegatingObject;
NSMutableDictionary *_handlers;
}
/** A dictionary of custom handlers to be used by custom responders
in a A2Dynamic(Protocol Name) subclass of A2DynamicDelegate, like
`A2DynamicUIAlertViewDelegate`.
*/
@property (nonatomic, retain, readonly) NSMutableDictionary *handlers;
/** The object that the dynamic delegate implements methods for. */
@property (nonatomic, assign, readonly) id delegatingObject;
/** @name Block Class Method Implementations */
/** The block that is to be fired when the specified
selector is called on the reciever.
@param selector An encoded selector. Must not be NULL.
@return A code block, or nil if no block is assigned.
*/
- (id) blockImplementationForMethod: (SEL) selector;
/** Assigns the given block to be fired when the specified
selector is called on the reciever.
[tableView.dynamicDataSource implementMethod:@selector(numberOfSectionsInTableView:)
withBlock:NSInteger^(UITableView *tableView){
return 2;
}];
@warning Starting with A2DynamicDelegate 2.0, a block will
not be checked for a matching signature. A block can have
less parameters than the original selector and will be
ignored, but cannot have more.
@param selector An encoded selector. Must not be NULL.
@param block A code block with the same signature as selector.
*/
- (void) implementMethod: (SEL) selector withBlock: (id) block;
/** Disassociates any block so that nothing will be fired
when the specified selector is called on the reciever.
@param selector An encoded selector. Must not be NULL.
*/
- (void) removeBlockImplementationForMethod: (SEL) selector;
/** @name Block Instance Method Implementations */
/** The block that is to be fired when the specified
selector is called on the delegating object's class.
@param selector An encoded selector. Must not be NULL.
@return A code block, or nil if no block is assigned.
*/
- (id) blockImplementationForClassMethod: (SEL) selector;
/** Assigns the given block to be fired when the specified
selector is called on the reciever.
@warning Starting with A2DynamicDelegate 2.0, a block will
not be checked for a matching signature. A block can have
less parameters than the original selector and will be
ignored, but cannot have more.
@param selector An encoded selector. Must not be NULL.
@param block A code block with the same signature as selector.
*/
- (void) implementClassMethod: (SEL) selector withBlock: (id) block;
/** Disassociates any blocks so that nothing will be fired
when the specified selector is called on the delegating
object's class.
@param selector An encoded selector. Must not be NULL.
*/
- (void) removeBlockImplementationForClassMethod: (SEL) selector;
@end
@interface NSObject (A2DynamicDelegate)
/** Creates or gets a dynamic data source for the reciever.
A2DynamicDelegate assumes a protocol name `FooBarDataSource`
for instances of class `FooBar`. The object is given a strong
attachment to the reciever, and is automatically deallocated
when the reciever is released.
If the user implements a `A2DynamicFooBarDataSource` subclass
of A2DynamicDelegate, its implementation of any method
will be used over the block. If the block needs to be used,
it can be called from within the custom
implementation using blockImplementationForMethod:.
@see blockImplementationForMethod:
@return A dynamic data source.
*/
- (id) dynamicDataSource;
/** Creates or gets a dynamic delegate for the reciever.
A2DynamicDelegate assumes a protocol name `FooBarDelegate`
for instances of class `FooBar`. The object is given a strong
attachment to the reciever, and is automatically deallocated
when the reciever is released.
If the user implements a `A2DynamicFooBarDelegate` subclass
of A2DynamicDelegate, its implementation of any method
will be used over the block. If the block needs to be used,
it can be called from within the custom
implementation using blockImplementationForMethod:.
@see blockImplementationForMethod:
@return A dynamic delegate.
*/
- (id) dynamicDelegate;
/** Creates or gets a dynamic protocol implementation for
the reciever. The designated initializer.
The object is given a strong attachment to the reciever,
and is automatically deallocated when the reciever is released.
If the user implements a subclass of A2DynamicDelegate prepended
with `A2Dynamic`, such as `A2DynamicFooProvider`, its
implementation of any method will be used over the block.
If the block needs to be used, it can be called from within the
custom implementation using blockImplementationForMethod:.
@param protocol A custom protocol.
@return A dynamic protocol implementation.
@see blockImplementationForMethod:
*/
- (id) dynamicDelegateForProtocol: (Protocol *) protocol;
@end