-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathBSModalPickerView.m
More file actions
102 lines (78 loc) · 2.57 KB
/
BSModalPickerView.m
File metadata and controls
102 lines (78 loc) · 2.57 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
//
// BSModalPickerView.m
// CustomPicker
//
// Created by Ben Scheirman on 7/16/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "BSModalPickerView.h"
@interface BSModalPickerView ()
@property (nonatomic) NSUInteger indexSelectedBeforeDismissal;
@end
@implementation BSModalPickerView
#pragma mark - Designated Initializer
- (id)initWithValues:(NSArray *)values {
self = [super init];
if (self) {
self.values = values;
self.userInteractionEnabled = YES;
}
return self;
}
#pragma mark - Custom Getters
- (UIView *)pickerWithFrame:(CGRect)pickerFrame {
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.backgroundColor = [UIColor whiteColor];
pickerView.dataSource = self;
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
[pickerView selectRow:self.selectedIndex inComponent:0 animated:NO];
return pickerView;
}
- (NSString *)selectedValue {
return [self.values objectAtIndex:self.selectedIndex];
}
#pragma mark - Custom Setters
- (void)setValues:(NSArray *)values {
_values = values;
if (_values) {
if (self.picker) {
UIPickerView *pickerView = (UIPickerView *)self.picker;
[pickerView reloadAllComponents];
self.selectedIndex = 0;
}
}
}
- (void)setSelectedIndex:(NSUInteger)selectedIndex {
if (_selectedIndex != selectedIndex) {
_selectedIndex = selectedIndex;
if (self.picker) {
UIPickerView *pickerView = (UIPickerView *)self.picker;
[pickerView selectRow:selectedIndex inComponent:0 animated:YES];
}
}
}
- (void)setSelectedValue:(NSString *)selectedValue {
NSInteger index = [self.values indexOfObject:selectedValue];
[self setSelectedIndex:index];
}
#pragma mark - Event Handler
- (void)onDone:(id)sender {
self.selectedIndex = self.indexSelectedBeforeDismissal;
[super onDone:sender];
}
#pragma mark - Picker View Data Source
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.values.count;
}
#pragma mark - Picker View Delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.values objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.indexSelectedBeforeDismissal = row;
}
@end