forked from ryanmaxwell/UIAlertView-Blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIAlertView+Blocks.m
239 lines (182 loc) · 8.59 KB
/
UIAlertView+Blocks.m
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
231
232
233
234
235
236
237
238
239
//
// UIAlertView+Blocks.m
// UIAlertViewBlocks
//
// Created by Ryan Maxwell on 29/08/13.
// Copyright (c) 2013 Ryan Maxwell. All rights reserved.
//
#import "UIAlertView+Blocks.h"
#import <objc/runtime.h>
static const void *UIAlertViewOriginalDelegateKey = &UIAlertViewOriginalDelegateKey;
static const void *UIAlertViewTapBlockKey = &UIAlertViewTapBlockKey;
static const void *UIAlertViewWillPresentBlockKey = &UIAlertViewWillPresentBlockKey;
static const void *UIAlertViewDidPresentBlockKey = &UIAlertViewDidPresentBlockKey;
static const void *UIAlertViewWillDismissBlockKey = &UIAlertViewWillDismissBlockKey;
static const void *UIAlertViewDidDismissBlockKey = &UIAlertViewDidDismissBlockKey;
static const void *UIAlertViewCancelBlockKey = &UIAlertViewCancelBlockKey;
static const void *UIAlertViewShouldEnableFirstOtherButtonBlockKey = &UIAlertViewShouldEnableFirstOtherButtonBlockKey;
@implementation UIAlertView (Blocks)
+ (void)showWithTitle:(NSString *)title
message:(NSString *)message
style :(UIAlertViewStyle) style
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSArray *)otherButtonTitles
tapBlock:(UIAlertViewCompletionBlock)tapBlock {
UIAlertView *alertView = [[self alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:nil];
alertView.alertViewStyle = style;
for (NSString *buttonTitle in otherButtonTitles) {
[alertView addButtonWithTitle:buttonTitle];
}
if (tapBlock) {
alertView.tapBlock = tapBlock;
}
[alertView show];
#if !__has_feature(objc_arc)
[alertView release];
#endif
}
+ (void)showWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSArray *)otherButtonTitles
tapBlock:(UIAlertViewCompletionBlock)tapBlock {
[self showWithTitle:title
message:message
style:UIAlertViewStyleDefault
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:otherButtonTitles
tapBlock:tapBlock];
}
#pragma mark -
- (void)_checkAlertViewDelegate {
if (self.delegate != (id<UIAlertViewDelegate>)self) {
objc_setAssociatedObject(self, UIAlertViewOriginalDelegateKey, self.delegate, OBJC_ASSOCIATION_ASSIGN);
self.delegate = (id<UIAlertViewDelegate>)self;
}
}
- (UIAlertViewCompletionBlock)tapBlock {
return objc_getAssociatedObject(self, UIAlertViewTapBlockKey);
}
- (void)setTapBlock:(UIAlertViewCompletionBlock)tapBlock {
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewTapBlockKey, tapBlock, OBJC_ASSOCIATION_COPY);
}
- (UIAlertViewCompletionBlock)willDismissBlock {
return objc_getAssociatedObject(self, UIAlertViewWillDismissBlockKey);
}
- (void)setWillDismissBlock:(UIAlertViewCompletionBlock)willDismissBlock {
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewWillDismissBlockKey, willDismissBlock, OBJC_ASSOCIATION_COPY);
}
- (UIAlertViewCompletionBlock)didDismissBlock {
return objc_getAssociatedObject(self, UIAlertViewDidDismissBlockKey);
}
- (void)setDidDismissBlock:(UIAlertViewCompletionBlock)didDismissBlock {
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewDidDismissBlockKey, didDismissBlock, OBJC_ASSOCIATION_COPY);
}
- (UIAlertViewBlock)willPresentBlock {
return objc_getAssociatedObject(self, UIAlertViewWillPresentBlockKey);
}
- (void)setWillPresentBlock:(UIAlertViewBlock)willPresentBlock {
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewWillPresentBlockKey, willPresentBlock, OBJC_ASSOCIATION_COPY);
}
- (UIAlertViewBlock)didPresentBlock {
return objc_getAssociatedObject(self, UIAlertViewDidPresentBlockKey);
}
- (void)setDidPresentBlock:(UIAlertViewBlock)didPresentBlock {
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewDidPresentBlockKey, didPresentBlock, OBJC_ASSOCIATION_COPY);
}
- (UIAlertViewBlock)cancelBlock {
return objc_getAssociatedObject(self, UIAlertViewCancelBlockKey);
}
- (void)setCancelBlock:(UIAlertViewBlock)cancelBlock {
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewCancelBlockKey, cancelBlock, OBJC_ASSOCIATION_COPY);
}
- (void)setShouldEnableFirstOtherButtonBlock:(BOOL(^)(UIAlertView *alertView))shouldEnableFirstOtherButtonBlock {
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewShouldEnableFirstOtherButtonBlockKey, shouldEnableFirstOtherButtonBlock, OBJC_ASSOCIATION_COPY);
}
- (BOOL(^)(UIAlertView *alertView))shouldEnableFirstOtherButtonBlock {
return objc_getAssociatedObject(self, UIAlertViewShouldEnableFirstOtherButtonBlockKey);
}
#pragma mark - UIAlertViewDelegate
- (void)willPresentAlertView:(UIAlertView *)alertView {
UIAlertViewBlock block = alertView.willPresentBlock;
if (block) {
block(alertView);
}
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(willPresentAlertView:)]) {
[originalDelegate willPresentAlertView:alertView];
}
}
- (void)didPresentAlertView:(UIAlertView *)alertView {
UIAlertViewBlock block = alertView.didPresentBlock;
if (block) {
block(alertView);
}
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(didPresentAlertView:)]) {
[originalDelegate didPresentAlertView:alertView];
}
}
- (void)alertViewCancel:(UIAlertView *)alertView {
UIAlertViewBlock block = alertView.cancelBlock;
if (block) {
block(alertView);
}
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertViewCancel:)]) {
[originalDelegate alertViewCancel:alertView];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
UIAlertViewCompletionBlock completion = alertView.tapBlock;
if (completion) {
completion(alertView, buttonIndex);
}
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
[originalDelegate alertView:alertView clickedButtonAtIndex:buttonIndex];
}
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
UIAlertViewCompletionBlock completion = alertView.willDismissBlock;
if (completion) {
completion(alertView, buttonIndex);
}
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertView:willDismissWithButtonIndex:)]) {
[originalDelegate alertView:alertView willDismissWithButtonIndex:buttonIndex];
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
UIAlertViewCompletionBlock completion = alertView.didDismissBlock;
if (completion) {
completion(alertView, buttonIndex);
}
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertView:didDismissWithButtonIndex:)]) {
[originalDelegate alertView:alertView didDismissWithButtonIndex:buttonIndex];
}
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
BOOL(^shouldEnableFirstOtherButtonBlock)(UIAlertView *alertView) = alertView.shouldEnableFirstOtherButtonBlock;
if (shouldEnableFirstOtherButtonBlock) {
return shouldEnableFirstOtherButtonBlock(alertView);
}
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertViewShouldEnableFirstOtherButton:)]) {
return [originalDelegate alertViewShouldEnableFirstOtherButton:alertView];
}
return YES;
}
@end