-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractActionSheetPicker.m
364 lines (316 loc) · 16 KB
/
AbstractActionSheetPicker.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
//Copyright (c) 2011, Tim Cinel
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions are met:
//* Redistributions of source code must retain the above copyright
//notice, this list of conditions and the following disclaimer.
//* Redistributions in binary form must reproduce the above copyright
//notice, this list of conditions and the following disclaimer in the
//documentation and/or other materials provided with the distribution.
//* Neither the name of the <organization> nor the
//names of its contributors may be used to endorse or promote products
//derived from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
//DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
//ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "AbstractActionSheetPicker.h"
#import <objc/message.h>
BOOL OSAtLeast(NSString* v) {
return [[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending;
}
@interface AbstractActionSheetPicker()
@property (nonatomic, strong) UIBarButtonItem *barButtonItem;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, unsafe_unretained) id target;
@property (nonatomic, assign) SEL successAction;
@property (nonatomic, assign) SEL cancelAction;
@property (nonatomic, strong) UIActionSheet *actionSheet;
@property (nonatomic, strong) UIPopoverController *popOverController;
@property (nonatomic, strong) NSObject *selfReference;
- (void)presentPickerForView:(UIView *)aView;
- (void)configureAndPresentPopoverForView:(UIView *)aView;
- (void)configureAndPresentActionSheetForView:(UIView *)aView;
- (void)presentActionSheet:(UIActionSheet *)actionSheet;
- (void)presentPopover:(UIPopoverController *)popover;
- (void)dismissPicker;
- (BOOL)isViewPortrait;
- (BOOL)isValidOrigin:(id)origin;
- (id)storedOrigin;
- (UIBarButtonItem *)createToolbarLabelWithTitle:(NSString *)aTitle;
- (UIToolbar *)createPickerToolbarWithTitle:(NSString *)aTitle;
- (UIBarButtonItem *)createButtonWithType:(UIBarButtonSystemItem)type target:(id)target action:(SEL)buttonAction;
- (IBAction)actionPickerDone:(id)sender;
- (IBAction)actionPickerCancel:(id)sender;
@end
@implementation AbstractActionSheetPicker
@synthesize title = _title;
@synthesize containerView = _containerView;
@synthesize barButtonItem = _barButtonItem;
@synthesize target = _target;
@synthesize successAction = _successAction;
@synthesize cancelAction = _cancelAction;
@synthesize actionSheet = _actionSheet;
@synthesize popOverController = _popOverController;
@synthesize selfReference = _selfReference;
@synthesize pickerView = _pickerView;
@dynamic viewSize;
@synthesize customButtons = _customButtons;
@synthesize hideCancel = _hideCancel;
@synthesize presentFromRect = _presentFromRect;
#pragma mark - Abstract Implementation
- (id)initWithTarget:(id)target successAction:(SEL)successAction cancelAction:(SEL)cancelActionOrNil origin:(id)origin {
self = [super init];
if (self) {
self.target = target;
self.successAction = successAction;
self.cancelAction = cancelActionOrNil;
self.presentFromRect = CGRectZero;
if ([origin isKindOfClass:[UIBarButtonItem class]])
self.barButtonItem = origin;
else if ([origin isKindOfClass:[UIView class]])
self.containerView = origin;
else
NSAssert(NO, @"Invalid origin provided to ActionSheetPicker ( %@ )", origin);
//allows us to use this without needing to store a reference in calling class
self.selfReference = self;
}
return self;
}
- (void)dealloc {
//need to clear picker delegates and datasources, otherwise they may call this object after it's gone
if ([self.pickerView respondsToSelector:@selector(setDelegate:)])
[self.pickerView performSelector:@selector(setDelegate:) withObject:nil];
if ([self.pickerView respondsToSelector:@selector(setDataSource:)])
[self.pickerView performSelector:@selector(setDataSource:) withObject:nil];
self.target = nil;
}
- (UIView *)configuredPickerView {
NSAssert(NO, @"This is an abstract class, you must use a subclass of AbstractActionSheetPicker (like ActionSheetStringPicker)");
return nil;
}
- (void)notifyTarget:(id)target didSucceedWithAction:(SEL)successAction origin:(id)origin {
NSAssert(NO, @"This is an abstract class, you must use a subclass of AbstractActionSheetPicker (like ActionSheetStringPicker)");
}
- (void)notifyTarget:(id)target didCancelWithAction:(SEL)cancelAction origin:(id)origin {
if (target && cancelAction && [target respondsToSelector:cancelAction]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[target performSelector:cancelAction withObject:origin];
#pragma clang diagnostic pop
}
}
#pragma mark - Actions
- (void)showActionSheetPicker {
UIView *masterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewSize.width, 260)];
self.toolbar = [self createPickerToolbarWithTitle:self.title];
[masterView addSubview: self.toolbar];
//ios7 picker draws a darkened alpha-only region on the first and last 8 pixels horizontally, but blurs the rest of its background. To make the whole popup appear to be edge-to-edge, we have to add blurring to the remaining left and right edges.
if (OSAtLeast(@"7.0")) {
CGRect f = CGRectMake(0,self.toolbar.frame.origin.y, 8, masterView.frame.size.height - self.toolbar.frame.origin.y);
UIToolbar* leftEdge = [[UIToolbar alloc] initWithFrame: f];
f.origin.x = masterView.frame.size.width - 8;
UIToolbar* rightEdge = [[UIToolbar alloc] initWithFrame: f];
leftEdge.barTintColor = rightEdge.barTintColor = self.toolbar.barTintColor;
[masterView insertSubview: leftEdge atIndex: 0];
[masterView insertSubview: rightEdge atIndex: 0];
}
self.pickerView = [self configuredPickerView];
NSAssert(_pickerView != NULL, @"Picker view failed to instantiate, perhaps you have invalid component data.");
[masterView addSubview:_pickerView];
[self presentPickerForView:masterView];
}
- (IBAction)actionPickerDone:(id)sender {
[self notifyTarget:self.target didSucceedWithAction:self.successAction origin:[self storedOrigin]];
[self dismissPicker];
}
- (IBAction)actionPickerCancel:(id)sender {
[self notifyTarget:self.target didCancelWithAction:self.cancelAction origin:[self storedOrigin]];
[self dismissPicker];
}
- (void)dismissPicker {
#if __IPHONE_4_1 <= __IPHONE_OS_VERSION_MAX_ALLOWED
if (self.actionSheet)
#else
if (self.actionSheet && [self.actionSheet isVisible])
#endif
[_actionSheet dismissWithClickedButtonIndex:0 animated:YES];
else if (self.popOverController && self.popOverController.popoverVisible)
[_popOverController dismissPopoverAnimated:YES];
self.actionSheet = nil;
self.popOverController = nil;
self.selfReference = nil;
}
#pragma mark - Custom Buttons
- (void)addCustomButtonWithTitle:(NSString *)title value:(id)value {
if (!self.customButtons)
_customButtons = [[NSMutableArray alloc] init];
if (!title)
title = @"";
if (!value)
value = [NSNumber numberWithInt:0];
NSDictionary *buttonDetails = [[NSDictionary alloc] initWithObjectsAndKeys:title, @"buttonTitle", value, @"buttonValue", nil];
[self.customButtons addObject:buttonDetails];
}
- (IBAction)customButtonPressed:(id)sender {
UIBarButtonItem *button = (UIBarButtonItem*)sender;
NSInteger index = button.tag;
NSAssert((index >= 0 && index < self.customButtons.count), @"Bad custom button tag: %ld, custom button count: %lu", (long)index, (unsigned long)self.customButtons.count);
NSAssert([self.pickerView respondsToSelector:@selector(selectRow:inComponent:animated:)], @"customButtonPressed not overridden, cannot interact with subclassed pickerView");
NSDictionary *buttonDetails = [self.customButtons objectAtIndex:index];
NSAssert(buttonDetails != NULL, @"Custom button dictionary is invalid");
NSInteger buttonValue = [[buttonDetails objectForKey:@"buttonValue"] intValue];
UIPickerView *picker = (UIPickerView *)self.pickerView;
NSAssert(picker != NULL, @"PickerView is invalid");
[picker selectRow:buttonValue inComponent:0 animated:YES];
if ([self respondsToSelector:@selector(pickerView:didSelectRow:inComponent:)]) {
void (*objc_msgSendTyped)(id self, SEL _cmd, id pickerView, NSInteger row, NSInteger component) = (void*)objc_msgSend; // sending Integers as params
objc_msgSendTyped(self, @selector(pickerView:didSelectRow:inComponent:), picker, buttonValue, 0);
}
}
- (UIToolbar *)createPickerToolbarWithTitle:(NSString *)title {
CGRect frame = CGRectMake(0, 0, self.viewSize.width, 44);
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:frame];
pickerToolbar.barStyle = OSAtLeast(@"7.0") ? UIBarStyleDefault : UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
NSInteger index = 0;
for (NSDictionary *buttonDetails in self.customButtons) {
NSString *buttonTitle = [buttonDetails objectForKey:@"buttonTitle"];
//NSInteger buttonValue = [[buttonDetails objectForKey:@"buttonValue"] intValue];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:buttonTitle style:UIBarButtonItemStyleBordered target:self action:@selector(customButtonPressed:)];
button.tag = index;
[barItems addObject:button];
index++;
}
if (NO == self.hideCancel) {
UIBarButtonItem *cancelBtn = [self createButtonWithType:UIBarButtonSystemItemCancel target:self action:@selector(actionPickerCancel:)];
[barItems addObject:cancelBtn];
}
UIBarButtonItem *flexSpace = [self createButtonWithType:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
if (title){
UIBarButtonItem *labelButton = [self createToolbarLabelWithTitle:title];
[barItems addObject:labelButton];
[barItems addObject:flexSpace];
}
UIBarButtonItem *doneButton = [self createButtonWithType:UIBarButtonSystemItemDone target:self action:@selector(actionPickerDone:)];
[barItems addObject:doneButton];
[pickerToolbar setItems:barItems animated:YES];
return pickerToolbar;
}
- (UIBarButtonItem *)createToolbarLabelWithTitle:(NSString *)aTitle {
UILabel *toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
[toolBarItemlabel setTextAlignment:NSTextAlignmentCenter];
[toolBarItemlabel setTextColor: OSAtLeast(@"7.0") ? [UIColor blackColor] : [UIColor whiteColor]];
[toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];
[toolBarItemlabel setBackgroundColor:[UIColor clearColor]];
toolBarItemlabel.text = aTitle;
UIBarButtonItem *buttonLabel = [[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
return buttonLabel;
}
- (UIBarButtonItem *)createButtonWithType:(UIBarButtonSystemItem)type target:(id)target action:(SEL)buttonAction {
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:type target:target action:buttonAction];
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
[barButton setTintColor: [[UIApplication sharedApplication] keyWindow].tintColor];
return barButton;
}
#pragma mark - Utilities and Accessors
- (CGSize)viewSize {
if (![self isViewPortrait])
return CGSizeMake(480, 320);
return CGSizeMake(320, 480);
}
- (BOOL)isViewPortrait {
return UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
}
- (BOOL)isValidOrigin:(id)origin {
if (!origin)
return NO;
BOOL isButton = [origin isKindOfClass:[UIBarButtonItem class]];
BOOL isView = [origin isKindOfClass:[UIView class]];
return (isButton || isView);
}
- (id)storedOrigin {
if (self.barButtonItem)
return self.barButtonItem;
return self.containerView;
}
#pragma mark - Popovers and ActionSheets
- (void)presentPickerForView:(UIView *)aView {
self.presentFromRect = aView.frame;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
[self configureAndPresentPopoverForView:aView];
else
[self configureAndPresentActionSheetForView:aView];
}
- (void)configureAndPresentActionSheetForView:(UIView *)aView {
NSString *paddedSheetTitle = nil;
CGFloat sheetHeight = self.viewSize.height - 47;
if ([self isViewPortrait]) {
paddedSheetTitle = @"\n\n\n"; // looks hacky to me
} else {
NSString *reqSysVer = @"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
sheetHeight = self.viewSize.width;
} else {
sheetHeight += 103;
}
}
_actionSheet = [[UIActionSheet alloc] initWithTitle:paddedSheetTitle delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[_actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[_actionSheet addSubview:aView];
[self presentActionSheet:_actionSheet];
_actionSheet.bounds = CGRectMake(0, 0, self.viewSize.width, sheetHeight);
}
- (void)presentActionSheet:(UIActionSheet *)actionSheet {
NSParameterAssert(actionSheet != NULL);
if (self.barButtonItem)
[actionSheet showFromBarButtonItem:_barButtonItem animated:YES];
else if (self.containerView && NO == CGRectIsEmpty(self.presentFromRect))
[actionSheet showFromRect:_presentFromRect inView:_containerView animated:YES];
else
[actionSheet showInView:_containerView];
}
- (void)configureAndPresentPopoverForView:(UIView *)aView {
UIViewController *viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
viewController.view = aView;
viewController.preferredContentSize = viewController.view.frame.size;
_popOverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
[self presentPopover:_popOverController];
}
- (void)presentPopover:(UIPopoverController *)popover {
NSParameterAssert(popover != NULL);
if (self.barButtonItem) {
[popover presentPopoverFromBarButtonItem:_barButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
return;
}
else if ((self.containerView)) {
[popover presentPopoverFromRect:_containerView.bounds inView:_containerView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
return;
}
// Unfortunately, things go to hell whenever you try to present a popover from a table view cell. These are failsafes.
UIView *origin = nil;
CGRect presentRect = CGRectZero;
@try {
origin = (_containerView.superview ? _containerView.superview : _containerView);
presentRect = origin.bounds;
[popover presentPopoverFromRect:presentRect inView:origin permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
@catch (NSException *exception) {
origin = [[[[UIApplication sharedApplication] keyWindow] rootViewController] view];
presentRect = CGRectMake(origin.center.x, origin.center.y, 1, 1);
[popover presentPopoverFromRect:presentRect inView:origin permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
@end