-
Notifications
You must be signed in to change notification settings - Fork 1
/
TLActionSheetController.m
169 lines (132 loc) · 5.11 KB
/
TLActionSheetController.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
//
// TLActionSheetController.m
// TLCommon
//
// Created by Joshua Bleecher Snyder on 11/10/09.
//
#import "TLActionSheetController.h"
#import "TLActionSheet.h"
#pragma mark -
@interface TLActionSheetController ()
- (NSInvocation *)invocationForTarget:(id)target action:(SEL)action;
@property(nonatomic, retain, readwrite) TLActionSheet *actionSheet;
@property(nonatomic, retain, readwrite) NSMutableArray *invocations;
@end
#pragma mark -
@implementation TLActionSheetController
@synthesize actionSheet;
@synthesize invocations;
#pragma mark -
#pragma mark Lifecycle
- (id)initWithTitle:(NSString *)title {
if(self = [super init]) {
self.actionSheet = [[[UIActionSheet alloc] initWithTitle:title
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil]
autorelease];
self.actionSheet.delegate = self;
self.invocations = [NSMutableArray array];
}
return self;
}
- (void)dealloc {
actionSheet.delegate = nil;
[actionSheet release];
actionSheet = nil;
[invocations release];
invocations = nil;
[super dealloc];
}
#pragma mark -
#pragma mark Add buttons
- (NSInvocation *)invocationForTarget:(id)target action:(SEL)action {
NSInvocation *invocation = nil;
if(target && action) {
NSMethodSignature *methodSignature = [target methodSignatureForSelector:action];
invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:target];
[invocation setSelector:action];
NSAssert1([methodSignature numberOfArguments] < 4, @"TLActionSheetController was given a target/action pair whose selector (%@) requires two or more arguments", NSStringFromSelector(action));
if([methodSignature numberOfArguments] == 1) {
[invocation setArgument:&self atIndex:2];
}
}
return invocation;
}
- (void)addButtonWithTitle:(NSString *)aTitle invocation:(NSInvocation *)anInvocation {
[self.actionSheet addButtonWithTitle:aTitle];
id invocation = anInvocation;
if(!invocation) {
invocation = [NSNull null];
}
[self.invocations addObject:invocation];
}
- (void)addButtonWithTitle:(NSString *)aTitle target:(id)aTarget action:(SEL)anAction {
[self addButtonWithTitle:aTitle
invocation:[self invocationForTarget:aTarget
action:anAction]];
}
- (void)addCancelButton {
[self addCancelButtonWithInvocation:nil];
}
- (void)addCancelButtonWithInvocation:(NSInvocation *)anInvocation {
[self addCancelButtonWithTitle:NSLocalizedString(@"Cancel", @"Action sheet title")
invocation:anInvocation];
}
- (void)addCancelButtonWithTarget:(id)aTarget action:(SEL)anAction {
[self addCancelButtonWithInvocation:[self invocationForTarget:aTarget action:anAction]];
}
- (void)addCancelButtonWithTitle:(NSString *)aTitle invocation:(NSInvocation *)anInvocation {
[self addButtonWithTitle:aTitle invocation:anInvocation];
self.actionSheet.cancelButtonIndex = self.actionSheet.numberOfButtons - 1;
}
- (void)addCancelButtonWithTitle:(NSString *)aTitle target:(id)aTarget action:(SEL)anAction {
[self addCancelButtonWithTitle:aTitle
invocation:[self invocationForTarget:aTarget
action:anAction]];
}
- (void)addDestructiveButtonWithTitle:(NSString *)aTitle invocation:(NSInvocation *)anInvocation {
[self addButtonWithTitle:aTitle invocation:anInvocation];
self.actionSheet.destructiveButtonIndex = self.actionSheet.numberOfButtons - 1;
}
- (void)addDestructiveButtonWithTitle:(NSString *)aTitle target:(id)aTarget action:(SEL)anAction {
[self addDestructiveButtonWithTitle:aTitle
invocation:[self invocationForTarget:aTarget
action:anAction]];
}
#pragma mark -
#pragma mark Show
- (void)showInView:(UIView *)view {
[self.actionSheet showInView:view];
// Ensure that the delegate (that's us) survives until the sheet is dismissed
[self retain];
}
- (void)showFromTabBar:(UITabBar *)tabBar {
[self.actionSheet showFromTabBar:tabBar];
// Ensure that the delegate (that's us) survives until the sheet is dismissed
[self retain];
}
- (void)showFromToolbar:(UIToolbar *)toolbar {
[self.actionSheet showFromToolbar:toolbar];
// Ensure that the delegate (that's us) survives until the sheet is dismissed
[self retain];
}
#pragma mark -
#pragma mark UIActionSheetDelegate methods
- (void)actionSheet:(UIActionSheet *)clickedActionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// Make sure this was headed to the right place
if(![clickedActionSheet isEqual:self.actionSheet]) {
return;
}
if(buttonIndex >= 0 && buttonIndex < [self.invocations count]) {
NSInvocation *invocation = [self.invocations objectAtIndex:buttonIndex];
if(![invocation isEqual:[NSNull null]]) {
[invocation invoke];
}
}
// Sheet to be dismissed, drop our self retain
[self autorelease];
}
@end