forked from skywinder/ActionSheetPicker-3.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionSheetCustomPicker.m
125 lines (100 loc) · 4.63 KB
/
ActionSheetCustomPicker.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
//
// ActionSheetPicker.m
// ActionSheetPicker
//
// Created by on 13/03/2012.
// Copyright (c) 2012 Club 15CC. All rights reserved.
//
#import "ActionSheetCustomPicker.h"
@interface ActionSheetCustomPicker ()
@property(nonatomic, strong) NSArray *initialSelections;
@end
@implementation ActionSheetCustomPicker
/////////////////////////////////////////////////////////////////////////
#pragma mark - Init
/////////////////////////////////////////////////////////////////////////
- (id)initWithTitle:(NSString *)title delegate:(id <ActionSheetCustomPickerDelegate>)delegate showCancelButton:(BOOL)showCancelButton origin:(id)origin
{
return [self initWithTitle:title delegate:delegate
showCancelButton:showCancelButton origin:origin
initialSelections:nil];
}
+ (id)showPickerWithTitle:(NSString *)title delegate:(id <ActionSheetCustomPickerDelegate>)delegate showCancelButton:(BOOL)showCancelButton origin:(id)origin
{
return [self showPickerWithTitle:title delegate:delegate showCancelButton:showCancelButton origin:origin
initialSelections:nil ];
}
- (id)initWithTitle:(NSString *)title delegate:(id <ActionSheetCustomPickerDelegate>)delegate showCancelButton:(BOOL)showCancelButton origin:(id)origin initialSelections:(NSArray *)initialSelections
{
if ( self = [self initWithTarget:nil successAction:nil cancelAction:nil origin:origin] )
{
self.title = title;
self.hideCancel = !showCancelButton;
NSAssert(delegate, @"Delegate can't be nil");
_delegate = delegate;
if (initialSelections)
self.initialSelections = [[NSArray alloc] initWithArray:initialSelections];
}
return self;
}
/////////////////////////////////////////////////////////////////////////
+ (id)showPickerWithTitle:(NSString *)title delegate:(id <ActionSheetCustomPickerDelegate>)delegate showCancelButton:(BOOL)showCancelButton origin:(id)origin initialSelections:(NSArray *)initialSelections
{
ActionSheetCustomPicker *picker = [[ActionSheetCustomPicker alloc] initWithTitle:title delegate:delegate
showCancelButton:showCancelButton origin:origin
initialSelections:initialSelections];
[picker showActionSheetPicker];
return picker;
}
/////////////////////////////////////////////////////////////////////////
#pragma mark - AbstractActionSheetPicker fulfilment
/////////////////////////////////////////////////////////////////////////
- (UIView *)configuredPickerView
{
CGRect pickerFrame = CGRectMake(0, 40, self.viewSize.width, 216);
UIPickerView *pv = [[UIPickerView alloc] initWithFrame:pickerFrame];
self.pickerView = pv;
// Default to our delegate being the picker's delegate and datasource
pv.delegate = _delegate;
pv.dataSource = _delegate;
pv.showsSelectionIndicator = YES;
if ( self.initialSelections )
{
NSAssert(pv.numberOfComponents == self.initialSelections.count, @"Number of sections not match");
for (NSUInteger i = 0; i < [self.initialSelections count]; i++)
{
NSInteger row = [(NSNumber *) self.initialSelections[i] integerValue];
NSAssert([pv numberOfRowsInComponent:i] > row, @"Number of sections not match");
[pv selectRow:row inComponent:i animated:NO];
// Strangely, the above selectRow:inComponent:animated: will not call
// pickerView:didSelectRow:inComponent: automatically, so we manually call it.
[pv reloadAllComponents];
}
}
// Allow the delegate to override and set additional configs
//to backward compatibility:
if ( [_delegate respondsToSelector:@selector(actionSheetPicker:configurePickerView:)] )
{
[_delegate actionSheetPicker:self configurePickerView:pv];
}
return pv;
}
/////////////////////////////////////////////////////////////////////////
- (void)notifyTarget:(id)target didSucceedWithAction:(SEL)successAction origin:(id)origin
{
// Ignore parent args and just notify the delegate
if ( [_delegate respondsToSelector:@selector(actionSheetPickerDidSucceed:origin:)] )
{
[_delegate actionSheetPickerDidSucceed:self origin:origin];
}
}
/////////////////////////////////////////////////////////////////////////
- (void)notifyTarget:(id)target didCancelWithAction:(SEL)cancelAction origin:(id)origin
{
// Ignore parent args and just notify the delegate
if ( [_delegate respondsToSelector:@selector(actionSheetPickerDidCancel:origin:)] )
{
[_delegate actionSheetPickerDidCancel:self origin:origin];
}
}
@end