//创建一个类,实现UIPickerView的基本功能
#import "PickerViewController.h"
// 遵守 UIPickerViewDataSource,UIPickerViewDelegate协议
@interface PickerViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
// 声明一个简单选择器
@property (nonatomic,strong) UIPickerView *picker;
// picker第一列数据的数据源数组
@property (nonatomic,strong) NSMutableArray *firstList;
// picker第二列数据的数据源数组
@property (nonatomic,strong) NSMutableArray *secondList;
// 用于显示简单选择器上选择的内容
@property (nonatomic,strong) UILabel *showLable;
// picker选择完点击按钮用于更新lable上的数据
@property (nonatomic,strong) UIButton *button;
// picker第一列选择的数据
@property (nonatomic,copy) NSString *firstStr;
// picker第二列选择的数据
@property (nonatomic,copy) NSString *secondStr;
@end
@implementation PickerViewController
// picker的懒加载,用于picker的初始化和设置
- (UIPickerView *)picker{
if (!_picker) {
self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(50, 100, self.view.bounds.size.width - 100, 40)];
_picker.shouldGroupAccessibilityChildren = YES;
_picker.delegate = self;
_picker.dataSource = self;
}
return _picker;
}
- (UIButton *)button{
if (!_button) {
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(150, 350, self.view.bounds.size.width - 300, 30);
[_button setTitle:@"确认" forState:UIControlStateNormal];
[_button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
_button.backgroundColor = [UIColor cyanColor];
}
return _button;
}
- (void)handleButtonAction:(UIButton *)sender{
self.showLable.text = [NSString stringWithFormat:@"%@ %@",self.firstStr,self.secondStr];
}
// 显示picker的选择数据
- (UILabel *)showLable{
if (!_showLable) {
self.showLable = [[UILabel alloc] initWithFrame:CGRectMake(50, 300, self.view.bounds.size.width - 100, 40)];
_showLable.backgroundColor = [UIColor yellowColor];
_showLable.textAlignment = NSTextAlignmentCenter;
_showLable.text = @"1 1";
}
return _showLable;
}
// 数据源的设置
- (NSMutableArray *)firstList{
if (!_firstList) {
self.firstList = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
}
return _firstList;
}
- (NSMutableArray *)secondList{
if (!_secondList) {
self.secondList = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
}
return _secondList;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 在view上添加控件
[self.view addSubview:self.picker];
[self.view addSubview:self.button];
[self.view addSubview:self.showLable];
// 给lable上的数据初始化
self.firstStr = @"1";
self.secondStr = @"1";
}
#pragma mark UIPickerViewDelegate
//pickerView的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
//返回每一列元素的个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == 0) {
return self.firstList.count;
}
return self.secondList.count;
}
//返回每一列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return 50;
}
//返回每一列中每行的元素
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0) {
return self.firstList[row];
} else {
return self.secondList[row];
}
}
//把数组中对应选择的行的字符串赋值字符串属性用于lable的显示
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (component == 0) {
self.firstStr = self.firstList[row];
} else {
self.secondStr = self.secondList[row];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end