详解iOS开发中UIPickerView控件的使用方法
简介
UIPickerView是iOS中常用的一种UI控件,用于显示一组可选的数据,并让用户从中选择一个。它通常是作为表单中的一部分,用于选择日期、时间、地址、性别等选项。本篇文章将详细介绍UIPickerView的使用方法。
基本用法
1. 创建UIPickerView对象
可以通过代码或xib/storyboard方式创建UIPickerView对象。以下是代码方式创建UIPickerView的方法:
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 216)];
2. 设置数据源
要让UIPickerView显示数据,需要设置数据源。数据源有两个部分,一部分是指定UIPickerView有多少列,另一部分是指定每列有多少行数据。数据源需要实现UIPickerViewDataSource协议中的两个方法:
// 返回UIPickerView的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// 返回指定列的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
以下是一个示例:
// 数据源
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2; // 两列数据
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return 3; // 第一列三行数据
} else {
return 5; // 第二列五行数据
}
}
3. 设置代理
要获取用户的选择结果以及自定义UIPickerView的外观,需要设置代理。代理需要实现UIPickerViewDelegate协议中的一些方法,例如:
// 返回指定列、行对应的视图,一般是UILabel
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view;
// 当用户选择一行时触发,可以在这里获取用户选择结果
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
以下是一个示例:
// 代理
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return [NSString stringWithFormat:@"数据1 %ld", (long)row];
} else {
return [NSString stringWithFormat:@"数据2 %ld", (long)row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"用户选择了第%ld列,第%ld行。(%@)", (long)component, (long)row, [self pickerView:pickerView titleForRow:row forComponent:component]);
}
4. 显示UIPickerView
将UIPickerView添加到视图中即可显示:
[self.view addSubview:pickerView];
示例1:选择日期
以下是一个用UIPickerView实现选择日期的示例:
@interface ViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) NSArray *years;
@property (strong, nonatomic) NSArray *months;
@property (strong, nonatomic) NSArray *days;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建UIPickerView对象
self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 216)];
self.pickerView.dataSource = self; // 设置数据源
self.pickerView.delegate = self; // 设置代理
[self.view addSubview:self.pickerView];
// 初始化数据
self.years = @[@"2020", @"2021", @"2022"];
self.months = @[@"01", @"02", @"03", @"04", @"05", @"06", @"07", @"08", @"09", @"10", @"11", @"12"];
self.days = @[@"01", @"02", @"03", @"04", @"05", @"06", @"07", @"08", @"09", @"10",
@"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20",
@"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31"];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3; // 年、月、日三列
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return self.years.count;
} else if (component == 1) {
return self.months.count;
} else {
return self.days.count;
}
}
#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return self.years[row];
} else if (component == 1) {
return self.months[row];
} else {
return self.days[row];
}
}
#pragma mark - 用户点击“确定”按钮
- (IBAction)buttonTapped:(id)sender {
NSString *year = self.years[[self.pickerView selectedRowInComponent:0]];
NSString *month = self.months[[self.pickerView selectedRowInComponent:1]];
NSString *day = self.days[[self.pickerView selectedRowInComponent:2]];
NSString *date = [NSString stringWithFormat:@"%@-%@-%@", year, month, day];
NSLog(@"%@", date);
}
@end
在该示例中,我们创建了一个用于选择日期的UIPickerView,包含了三列数据分别表示年、月、日。并在“确定”按钮被点击时获取了用户的选择结果。
示例2:选择性别
以下是一个用UIPickerView实现选择性别的示例:
@interface ViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) NSArray *sexList;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建UIPickerView对象
self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 216)];
self.pickerView.dataSource = self; // 设置数据源
self.pickerView.delegate = self; // 设置代理
[self.view addSubview:self.pickerView];
// 初始化数据
self.sexList = @[@"男", @"女"];
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.sexList.count;
}
#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return self.sexList[row];
}
#pragma mark - 用户点击“确定”按钮
- (IBAction)buttonTapped:(id)sender {
NSString *sex = self.sexList[[self.pickerView selectedRowInComponent:0]];
NSLog(@"%@", sex);
}
@end
在该示例中,我们创建了一个用于选择性别的UIPickerView,包含了一列数据。并在“确定”按钮被点击时获取了用户的选择结果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解iOS开发中UIPickerView控件的使用方法 - Python技术站