forked from BlocksKit/BlocksKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIAlertView+BlocksKit.m
More file actions
230 lines (178 loc) · 6.81 KB
/
Copy pathUIAlertView+BlocksKit.m
File metadata and controls
230 lines (178 loc) · 6.81 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// UIAlertView+BlocksKit.m
// BlocksKit
//
#import "UIAlertView+BlocksKit.h"
#import "A2DynamicDelegate.h"
#import "NSObject+A2BlockDelegate.h"
#import "NSObject+A2DynamicDelegate.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma mark Delegate
@interface A2DynamicUIAlertViewDelegate : A2DynamicDelegate <UIAlertViewDelegate>
@end
@implementation A2DynamicUIAlertViewDelegate
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
BOOL should = YES;
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(alertViewShouldEnableFirstOtherButton:)])
should &= [realDelegate alertViewShouldEnableFirstOtherButton:alertView];
BOOL (^block)(UIAlertView *) = [self blockImplementationForMethod:_cmd];
if (block)
should &= block(alertView);
return should;
}
- (void)alertViewCancel:(UIAlertView *)alertView
{
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(alertViewCancel:)])
[realDelegate alertViewCancel:alertView];
id key = @(alertView.cancelButtonIndex);
void (^cancelBlock)(void) = (self.handlers)[key];
if (cancelBlock)
cancelBlock();
}
- (void)willPresentAlertView:(UIAlertView *)alertView
{
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(willPresentAlertView:)])
[realDelegate willPresentAlertView:alertView];
void (^block)(UIAlertView *) = [self blockImplementationForMethod:_cmd];
if (block)
block(alertView);
}
- (void)didPresentAlertView:(UIAlertView *)alertView
{
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(didPresentAlertView:)])
[realDelegate didPresentAlertView:alertView];
void (^block)(UIAlertView *) = [self blockImplementationForMethod:_cmd];
if (block)
block(alertView);
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(alertView:willDismissWithButtonIndex:)])
[realDelegate alertView:alertView willDismissWithButtonIndex:buttonIndex];
void (^block)(UIAlertView *, NSInteger) = [self blockImplementationForMethod:_cmd];
if (block)
block(alertView, buttonIndex);
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(alertView:didDismissWithButtonIndex:)])
[realDelegate alertView:alertView didDismissWithButtonIndex:buttonIndex];
void (^block)(UIAlertView *, NSInteger) = [self blockImplementationForMethod:_cmd];
if (block)
block(alertView, buttonIndex);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
id realDelegate = self.realDelegate;
if (realDelegate && [realDelegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
[realDelegate alertView:alertView clickedButtonAtIndex:buttonIndex];
void (^block)(UIAlertView *, NSInteger) = [self blockImplementationForMethod:_cmd];
if (block)
block(alertView, buttonIndex);
id key = @(buttonIndex);
void (^buttonBlock)(void) = (self.handlers)[key];
if (buttonBlock)
buttonBlock();
}
@end
#pragma mark - Category
@implementation UIAlertView (BlocksKit)
@dynamic bk_willShowBlock, bk_didShowBlock, bk_willDismissBlock, bk_didDismissBlock, bk_shouldEnableFirstOtherButtonBlock;
+ (void)load
{
@autoreleasepool {
[self bk_registerDynamicDelegate];
[self bk_linkDelegateMethods:@{
@"bk_willShowBlock": @"willPresentAlertView:",
@"bk_didShowBlock": @"didPresentAlertView:",
@"bk_willDismissBlock": @"alertView:willDismissWithButtonIndex:",
@"bk_didDismissBlock": @"alertView:didDismissWithButtonIndex:",
@"bk_shouldEnableFirstOtherButtonBlock": @"alertViewShouldEnableFirstOtherButton:"
}];
}
}
#pragma mark Convenience
+ (instancetype)bk_showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles handler:(void (^)(UIAlertView *alertView, NSInteger buttonIndex))block
{
// If no buttons were specified, cancel button becomes "Dismiss"
if (!cancelButtonTitle.length && !otherButtonTitles.count)
cancelButtonTitle = NSLocalizedString(@"Dismiss", nil);
UIAlertView *alertView = [[[self class] alloc] initWithTitle:title message:message delegate:self.bk_dynamicDelegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
// Set other buttons
[otherButtonTitles enumerateObjectsUsingBlock:^(NSString *button, NSUInteger idx, BOOL *stop) {
[alertView addButtonWithTitle:button];
}];
// Set `didDismissBlock`
if (block) alertView.bk_didDismissBlock = block;
// Show alert view
[alertView show];
return alertView;
}
#pragma mark Initializers
+ (instancetype)bk_alertViewWithTitle:(NSString *)title
{
return [self bk_alertViewWithTitle:title message:nil];
}
+ (instancetype)bk_alertViewWithTitle:(NSString *)title message:(NSString *)message
{
return [[[self class] alloc] bk_initWithTitle:title message:message];
}
- (instancetype)bk_initWithTitle:(NSString *)title message:(NSString *)message
{
self = [self initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
if (!self) return nil;
self.delegate = self.bk_dynamicDelegate;
return self;
}
#pragma Actions
- (NSInteger)bk_addButtonWithTitle:(NSString *)title handler:(void (^)(void))block
{
NSAssert(title.length, @"A button without a title cannot be added to the alert view.");
NSInteger index = [self addButtonWithTitle:title];
[self bk_setHandler:block forButtonAtIndex:index];
return index;
}
- (NSInteger)bk_setCancelButtonWithTitle:(NSString *)title handler:(void (^)(void))block
{
if (!title.length)
title = NSLocalizedString(@"Cancel", nil);
NSInteger cancelButtonIndex = [self addButtonWithTitle:title];
self.cancelButtonIndex = cancelButtonIndex;
[self bk_setHandler:block forButtonAtIndex:cancelButtonIndex];
return cancelButtonIndex;
}
#pragma mark Properties
- (void)bk_setHandler:(void (^)(void))block forButtonAtIndex:(NSInteger)index
{
id key = @(index);
if (block)
[self.bk_dynamicDelegate handlers][key] = [block copy];
else
[[self.bk_dynamicDelegate handlers] removeObjectForKey:key];
}
- (void (^)(void))bk_handlerForButtonAtIndex:(NSInteger)index
{
return [self.bk_dynamicDelegate handlers][@(index)];
}
- (void (^)(void))bk_cancelBlock
{
return [self bk_handlerForButtonAtIndex:self.cancelButtonIndex];
}
- (void)bk_setCancelBlock:(void (^)(void))block
{
if (block && self.cancelButtonIndex == -1) {
[self bk_setCancelButtonWithTitle:nil handler:block];
return;
}
[self bk_setHandler:block forButtonAtIndex:self.cancelButtonIndex];
}
@end
#pragma clang diagnostic pop