关于UIPickerView

  • UIPickerView是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活。UIPickerView直接继承了UIView,没有继承UIControl,因此,它不能像UIControl那样绑定事件处理方法,UIPickerView的事件处理由其委托对象完成。

  • UIDatePicker控件只是负责该控件的通用行为,而该控件包含多少列,各列包含多少个列表项则由UIPickerViewDataSource对象负责。开发者必须为UIPickerView设置UIPickerViewDataSource对象,并实现如下两个方法。

    1
    - numberOfComponentsInPickerView:
  • 该UIPickerView将通过该方法来判断应该包含多少列。

    1
    - pickerView:numberOfRowsInComponent:
  • 该UIPickerView将通过该方法判断指定列应该包含多少个列表项。

  • 如果程序需要控制UIPickerView中各列的宽度,以及各列中列表项的大小和外观,或程序需要为UIPickerView的选中事件提供响应,都需要为UIPickerView设置UIPickerViewDelegate委托对象,并根据需要实现该委托对象中的如下方法。

    1
    - pickerView:rowHeightForComponent:
  • 该方法返回的CGFloat值将作为该UIPickerView控件中指定列中列表项的高度。

    1
    - pickerView:widthForComponent:
  • 该方法返回的CGFloat值将作为该UIPickerView控件中指定列的宽度。

    1
    - pickerView:titleForRow:forComponent:
  • 该方法返回的NSString值将作为该UIPickerView控件中指定列的列表项的文本标题。

    1
    - pickerView:viewForRow:forComponent:reusingView:
  • 该方法返回的UIView控件将直接作为该UIPickerView控件中指定列的指定列表项。

    1
    - pickerView:didSelectRow:inComponent:
  • 当用户单击选中该UIPickerView控件的指定列的指定列表项时将会激发该方法。

- example:此处为关联选择的选择器

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
#import "ViewController.h"
#define kStateComponent 0
#define kZipcomponent 1
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
// firstArr为第一列的数据,secondArr为第二列的数据
@property (strong, nonatomic) UIPickerView *pick;
//dataDic用于存储两列的数据
@property (strong, nonatomic) NSDictionary *dataDic;
@property (nonatomic, strong) NSArray *firstArr;
@property (nonatomic,strong) NSArray *secondArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
// 创建一个UIPickerView对象,并设置代理
_pick = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 210, 320, 80)];
_pick.dataSource = self;
_pick.delegate = self;
_pick.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_pick];
// 获取数据
NSBundle *bundle = [NSBundle mainBundle];
NSURL *plistURL = [bundle URLForResource:@"statedictionary" withExtension:@"plist"];
self.dataDic = [NSDictionary dictionaryWithContentsOfURL:plistURL];
NSArray *allData = [self.dataDic allKeys];
NSArray *sortedStates = [allData sortedArrayUsingSelector:@selector(compare:)];
self.firstArr = sortedStates;
// 获取字典里的第一个元素的key对应的value(即一个数组)作为第二列的默认数据
NSString *selectedState = self.firstArr[0];
self.secondArr = self.dataDic[selectedState];
}
#pragma mark -Picker Datasource
// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
// 每列的个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == kStateComponent) {
return [self.firstArr count];
} else {
return [self.secondArr count];
}
}
#pragma mark - Picker Delegate
// 返回当前行的内容,确定每个轮子的每一项显示的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == kStateComponent) {
return self.firstArr[row];
} else {
return self.secondArr[row];
}
}
// 监听轮子的滚动
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == kStateComponent) {
NSString * selectedState = self.firstArr[row];
self.secondArr = self.dataDic[selectedState];
// !!! 更新第二个轮子的数据
[_pick reloadComponent:kZipcomponent];
[_pick selectRow:0 inComponent:kZipcomponent animated:YES];
}
// 获取UIPickerView的不同的component选中的那行
NSInteger stateRow = [self.pick selectedRowInComponent:kStateComponent];
NSInteger zipRow = [self.pick selectedRowInComponent:kZipcomponent];
NSString *state = self.firstArr[stateRow];
NSString *zip = self.secondArr[zipRow];
NSString * title = [[NSString alloc] initWithFormat:@"You selected zip code %@",zip];
NSString * message = [[NSString alloc] initWithFormat:@"%@ is in %@",zip,state];
//官方解释:UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert. (主要是说UIAlertView在iOS 8中被废弃了,取而代之的是UIAlertController)
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
// 每列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat pickerWidth = _pick.bounds.size.width;
if (component == kZipcomponent) {
return pickerWidth / 3;
} else {
return 2 * pickerWidth / 3;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end