为了用户输入的统一性,选择器就出现了。
日期选择器
日期是最复杂的有各种各样的格式,为了统一iOS推出了UIDatePicker。日期选择器有4种模式:日期、日期时间、时间和倒计时定时器。
下面这个Demo是通过时间选择器选择一个时间,然后点击按钮,将所选时间显示。
界面如下:
代码如下:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)onclick:(id)sender;
@end
@implementation ViewController
- (IBAction)onclick:(id)sender {
NSDate *theDate = self.datePicker.date;
NSLog(@"the date picked is: %@",[theDate descriptionWithLocale:[NSLocale currentLocale]]);
//NSDate的descriptionWithLocale的方法是返回本地化的日期格式
//NSLocale的currentLocale静态方法是返回当前对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSLog(@"the date formate is:%@",[dateFormatter stringFromDate:theDate]);
self.label.text = [dateFormatter stringFromDate:theDate];
}
@end
普通选择器
有时候我们需要输入其他内容时也需要使用到选择器来规范用户的输入,这时候就需要用到普通选择器。普通选择器并不是日期选择器的父类,它十分灵活,可以设置拨盘的数量,每个拨盘的内容也可以自己设定。所以这里就需要用到代理协议——UIPickerViewDelegate和UIPickerViewDataSource。
下面这个Demo是通过普通选择器选择籍贯,然后点击按钮打印。
界面如下:
代码如下:
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
NSDictionary *_pickerData; //总数据
NSArray *_pickerProvincesData; //第一个拨盘省份数据
NSArray *_pickerCitiesData; //第二个拨盘城市数据
}
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)onclick:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
//解析数据
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"provinces_cities.plist" ofType:nil];
_pickerData = [[NSDictionary alloc]initWithContentsOfFile:filePath];
_pickerProvincesData = [_pickerData allKeys];
NSString *selectedProvince = [_pickerProvincesData objectAtIndex:0];
_pickerCitiesData = [_pickerData objectForKey:selectedProvince];
self.pickerView.dataSource =self;
self.pickerView.delegate = self;
}
#pragma mark - pickerView数据源协议方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2; //拨盘数量
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == 0) {//第一个拨盘
return _pickerProvincesData.count;
} else {
return _pickerCitiesData.count;
}
}
#pragma mark - pickerView代理协议方法
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//为选择器中某个拨盘的行提供数据
if (component == 0) {
return [_pickerProvincesData objectAtIndex:row];
} else {
return [_pickerCitiesData objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//选中选择器的某个拨盘中的某行时调用
if (component == 0) {
NSString *selectedProvince = [_pickerProvincesData objectAtIndex:row];
NSArray *array = [_pickerData objectForKey:selectedProvince];
_pickerCitiesData = array;
[self.pickerView reloadComponent:1];
}
}
- (IBAction)onclick:(id)sender {
NSInteger row1 = [self.pickerView selectedRowInComponent:0];
NSInteger row2 = [self.pickerView selectedRowInComponent:1];
NSString *selected1 = [_pickerProvincesData objectAtIndex:row1];
NSString *selected2 = [_pickerCitiesData objectAtIndex:row2];
self.label.text = [[NSString alloc] initWithFormat:@"%@,%@市",selected1,selected2];
}
@end
网友评论