forked from TimCinel/ActionSheetPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionSheetPicker.m
executable file
·269 lines (211 loc) · 8.71 KB
/
ActionSheetPicker.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
//
// ActionSheetPicker.m
// Spent
//
// Created by Tim Cinel on 3/01/11.
// Copyright 2011 Thunderous Playground. All rights reserved.
//
#import "ActionSheetPicker.h"
@implementation ActionSheetPicker
@synthesize view = _view;
@synthesize data = _data;
@synthesize selectedIndex = _selectedIndex;
@synthesize title = _title;
@synthesize selectedDate = _selectedDate;
@synthesize datePickerMode = _datePickerMode;
@synthesize target = _target;
@synthesize action = _action;
@synthesize actionSheet = _actionSheet;
@synthesize popOverController = _popOverController;
@synthesize pickerView = _pickerView;
@synthesize datePickerView = _datePickerView;
@synthesize pickerPosition = _pickerPosition;
@dynamic viewSize;
#pragma mark -
#pragma mark NSObject
+ (void)displayActionPickerWithView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title {
ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDataWithContainingView:aView data:data selectedIndex:selectedIndex target:target action:action title:title];
[actionSheetPicker showActionPicker];
[actionSheetPicker release];
}
+ (void)displayActionPickerWithView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title {
ActionSheetPicker *actionSheetPicker = [[ActionSheetPicker alloc] initForDateWithContainingView:aView datePickerMode:datePickerMode selectedDate:selectedDate target:target action:action title:title];
[actionSheetPicker showActionPicker];
[actionSheetPicker release];
}
- (id)initWithContainingView:(UIView *)aView target:(id)target action:(SEL)action {
if ((self = [super init]) != nil) {
self.view = aView;
self.target = target;
self.action = action;
}
return self;
}
- (id)initForDataWithContainingView:(UIView *)aView data:(NSArray *)data selectedIndex:(NSInteger)selectedIndex target:(id)target action:(SEL)action title:(NSString *)title {
if ([self initWithContainingView:aView target:target action:action] != nil) {
self.data = data;
self.selectedIndex = selectedIndex;
self.title = title;
}
return self;
}
- (id)initForDateWithContainingView:(UIView *)aView datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action title:(NSString *)title {
if ([self initWithContainingView:aView target:target action:action] != nil) {
self.datePickerMode = datePickerMode;
self.selectedDate = selectedDate;
self.title = title;
}
return self;
}
#pragma mark -
#pragma mark Implementation
- (void)showActionPicker {
[self retain];
//create the new view
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewSize.width, 260)] autorelease];
if (nil != self.data) {
//show data picker
[self showDataPicker];
[view addSubview:self.pickerView];
} else {
//show date picker
[self showDatePicker];
[view addSubview:self.datePickerView];
}
CGRect frame = CGRectMake(0, 0, self.viewSize.width, 44);
UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:frame];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(actionPickerCancel)];
[barItems addObject:cancelBtn];
[cancelBtn release];
UIBarButtonItem *flexSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil] autorelease];
[barItems addObject:flexSpace];
//Add tool bar title label
if (nil != self.title){
UILabel *toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
[toolBarItemlabel setTextAlignment:UITextAlignmentCenter];
[toolBarItemlabel setTextColor:[UIColor whiteColor]];
[toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];
[toolBarItemlabel setBackgroundColor:[UIColor clearColor]];
toolBarItemlabel.text = self.title;
UIBarButtonItem *buttonLabel =[[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
[toolBarItemlabel release];
[barItems addObject:buttonLabel];
[buttonLabel release];
[barItems addObject:flexSpace];
}
//add "Done" button
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(actionPickerDone)];
[barItems addObject:barButton];
[barButton release];
[pickerDateToolbar setItems:barItems animated:YES];
[barItems release];
[view addSubview:pickerDateToolbar];
[pickerDateToolbar release];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//spawn popovercontroller
UIViewController *viewController = [[[UIViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController.view = view;
viewController.contentSizeForViewInPopover = viewController.view.frame.size;
_popOverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
[self.popOverController presentPopoverFromRect:self.view.frame inView:self.view.superview?:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
//spawn actionsheet
_actionSheet = [[UIActionSheet alloc] initWithTitle:[self isViewPortrait]?nil:@"\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[self.actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[self.actionSheet addSubview:view];
[self.actionSheet showInView:self.view];
self.actionSheet.bounds = CGRectMake(0, 0, self.viewSize.width, self.viewSize.height+5);
}
}
- (void)showDataPicker {
//spawn pickerview
CGRect pickerFrame = CGRectMake(0, 40, self.viewSize.width, 216);
_pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.showsSelectionIndicator = YES;
[self.pickerView selectRow:self.selectedIndex inComponent:0 animated:NO];
}
- (void)showDatePicker {
//spawn datepickerview
CGRect datePickerFrame = CGRectMake(0, 40, self.viewSize.width, 216);
_datePickerView = [[UIDatePicker alloc] initWithFrame:datePickerFrame];
self.datePickerView.datePickerMode = self.datePickerMode;
[self.datePickerView setDate:self.selectedDate animated:NO];
[self.datePickerView addTarget:self action:@selector(eventForDatePicker:) forControlEvents:UIControlEventValueChanged];
}
- (void)actionPickerDone {
if (self.actionSheet) {
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
} else {
[self.popOverController dismissPopoverAnimated:YES];
}
if (nil != self.data) {
//send data picker message
[self.target performSelector:self.action withObject:[NSNumber numberWithInt:self.selectedIndex] withObject:self.view];
} else {
//send date picker message
[self.target performSelector:self.action withObject:self.selectedDate withObject:self.view];
}
[self release];
}
- (void)actionPickerCancel {
if (self.actionSheet) {
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
} else {
[self.popOverController dismissPopoverAnimated:YES];
}
[self release];
}
- (BOOL) isViewPortrait {
return UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
}
- (CGSize) viewSize {
CGSize size = CGSizeMake(320, 480);
if (![self isViewPortrait]) {
size = CGSizeMake(480, 320);
}
return size;
}
#pragma mark -
#pragma mark Callbacks
- (void)eventForDatePicker:(id)sender {
UIDatePicker *datePicker = (UIDatePicker *)sender;
self.selectedDate = datePicker.date;
}
#pragma mark -
#pragma mark UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.selectedIndex = row;
}
#pragma mark -
#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.data.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.data objectAtIndex:row];
}
#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
// NSLog(@"ActionSheet Dealloc");
self.actionSheet = nil;
self.popOverController = nil;
self.data = nil;
self.pickerView.delegate = nil;
self.pickerView.dataSource = nil;
self.pickerView = nil;
[self.datePickerView removeTarget:self action:@selector(eventForDatePicker:) forControlEvents:UIControlEventValueChanged];
self.datePickerView = nil;
self.selectedDate = nil;
self.view = nil;
self.target = nil;
[super dealloc];
}
@end