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
executable file
·185 lines (142 loc) · 6.07 KB
/
Copy pathUIAlertView+BlocksKit.m
File metadata and controls
executable file
·185 lines (142 loc) · 6.07 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
//
// UIAlertView+BlocksKit.m
// BlocksKit
//
#import "UIAlertView+BlocksKit.h"
#import "NSObject+AssociatedObjects.h"
@interface UIAlertView (BlocksKitPrivate)
@property (nonatomic, retain) NSMutableDictionary *blocks;
@end
@implementation UIAlertView (BlocksKit)
static char *kAlertViewBlockDictionaryKey = "UIAlertViewBlockHandlers";
static NSString *kAlertViewWillShowBlockKey = @"UIAlertViewWillShowBlock";
static NSString *kAlertViewDidShowBlockKey = @"UIAlertViewDidShowBlock";
static NSString *kAlertViewWillDismissBlockKey = @"UIAlertViewWillDismissBlock";
static NSString *kAlertViewDidDismissBlockKey = @"UIAlertViewDidDismissBlock";
#pragma mark Initializers
+ (id)alertWithTitle:(NSString *)title {
return [self alertWithTitle:title message:nil];
}
+ (id)alertWithTitle:(NSString *)title message:(NSString *)message {
return BK_AUTORELEASE([[UIAlertView alloc] initWithTitle:title message:message]);
}
- (id)initWithTitle:(NSString *)title message:(NSString *)message {
return [self initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
}
#pragma mark Public methods
- (NSInteger)addButtonWithTitle:(NSString *)title handler:(BKBlock)block {
if (!self.delegate)
self.delegate = self;
NSAssert([self.delegate isEqual:self], @"A block-backed button cannot be added when the delegate isn't self.");
NSAssert(title.length, @"A button without a title cannot be added to the alert view.");
NSInteger index = [self addButtonWithTitle:title];
BKBlock handler = BK_AUTORELEASE([block copy]);
[self.blocks setObject:handler forKey:[NSNumber numberWithInteger:index]];
return index;
}
- (NSInteger)setCancelButtonWithTitle:(NSString *)title handler:(BKBlock)block {
if (!self.delegate)
self.delegate = self;
NSAssert([self.delegate isEqual:self], @"A block-backed button cannot be added when the delegate isn't self.");
if (!title) title = NSLocalizedString(@"Cancel", nil);
NSInteger index = [self addButtonWithTitle:title];
self.cancelButtonIndex = index;
BKBlock handler = BK_AUTORELEASE([block copy]);
[self.blocks setObject:handler forKey:[NSNumber numberWithInteger:index]];
return index;
}
#pragma mark Properties
- (NSMutableDictionary *)blocks {
NSMutableDictionary *blocks = [self associatedValueForKey:kAlertViewBlockDictionaryKey];
if (!blocks) {
blocks = [NSMutableDictionary dictionary];
[self associateValue:blocks withKey:kAlertViewBlockDictionaryKey];
}
return blocks;
}
- (void)setBlocks:(NSMutableDictionary *)blocks {
[self associateValue:blocks withKey:kAlertViewBlockDictionaryKey];
}
- (BKBlock)cancelBlock {
NSNumber *key = [NSNumber numberWithInteger:self.cancelButtonIndex];
return [self.blocks objectForKey:key];
}
- (void)setCancelBlock:(BKBlock)block {
if (!self.delegate)
self.delegate = self;
NSAssert([self.delegate isEqual:self], @"A block-backed button cannot be added when the delegate isn't self.");
if (self.cancelButtonIndex == -1) {
[self setCancelButtonWithTitle:nil handler:block];
} else {
BKBlock handler = BK_AUTORELEASE([block copy]);
NSNumber *key = [NSNumber numberWithInteger:self.cancelButtonIndex];
[self.blocks setObject:handler forKey:key];
}
}
- (BKBlock)willShowBlock {
return [self.blocks objectForKey:kAlertViewWillShowBlockKey];
}
- (void)setWillShowBlock:(BKBlock)block {
if (!self.delegate)
self.delegate = self;
NSAssert([self.delegate isEqual:self], @"A block-backed button cannot be added when the delegate isn't self.");
BKBlock handler = BK_AUTORELEASE([block copy]);
[self.blocks setObject:handler forKey:kAlertViewWillShowBlockKey];
}
- (BKBlock)didShowBlock {
return [self.blocks objectForKey:kAlertViewDidShowBlockKey];
}
- (void)setDidShowBlock:(BKBlock)block {
if (!self.delegate)
self.delegate = self;
NSAssert([self.delegate isEqual:self], @"A block-backed button cannot be added when the delegate isn't self.");
BKBlock handler = BK_AUTORELEASE([block copy]);
[self.blocks setObject:handler forKey:kAlertViewDidShowBlockKey];
}
- (BKIndexBlock)willDismissBlock {
return [self.blocks objectForKey:kAlertViewWillDismissBlockKey];
}
- (void)setWillDismissBlock:(BKIndexBlock)block {
if (!self.delegate)
self.delegate = self;
NSAssert([self.delegate isEqual:self], @"A block-backed button cannot be added when the delegate isn't self.");
BKIndexBlock handler = BK_AUTORELEASE([block copy]);
[self.blocks setObject:handler forKey:kAlertViewWillDismissBlockKey];
}
- (BKIndexBlock)didDismissBlock {
return [self.blocks objectForKey:kAlertViewDidDismissBlockKey];
}
- (void)setDidDismissBlock:(BKIndexBlock)block {
if (!self.delegate)
self.delegate = self;
NSAssert([self.delegate isEqual:self], @"A block-backed button cannot be added when the delegate isn't self.");
BKIndexBlock handler = BK_AUTORELEASE([block copy]);
[self.blocks setObject:handler forKey:kAlertViewDidDismissBlockKey];
}
#pragma mark Delegates
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
BKBlock block = [self.blocks objectForKey:[NSNumber numberWithInteger:buttonIndex]];
if (block)
block();
}
- (void)willPresentAlertView:(UIAlertView *)alertView {
BKBlock block = [self.blocks objectForKey:kAlertViewWillShowBlockKey];
if (block)
block();
}
- (void)didPresentAlertView:(UIAlertView *)alertView {
BKBlock block = [self.blocks objectForKey:kAlertViewDidShowBlockKey];
if (block)
block();
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
BKIndexBlock block = [self.blocks objectForKey:kAlertViewWillDismissBlockKey];
if (block)
block(buttonIndex);
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
BKIndexBlock block = [self.blocks objectForKey:kAlertViewDidDismissBlockKey];
if (block)
block(buttonIndex);
}
@end