简述
在应用的一些设置中经常要用到一些Picker来快速帮助用户选定取值,一般会用到的有UIDatePicker,UIPickerView以及UIImagePickerController。
初始界面
如图,在搭建好的界面,实现方法来完成各个选择。首先是日期选择
UIDatePicker
主要需要设置的属性有
locale
datePickerMode
UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_cn"]; datePicker.datePickerMode = UIDatePickerModeDate; NSString *stdDate = @"1990-01-01"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd"; [datePicker setDate:[formatter dateFromString:stdDate] animated:YES]; [datePicker addTarget:self action:@selector(showDate:) forControlEvents:UIControlEventValueChanged]; _dateField.inputView = datePicker; _dateField.text = stdDate;
在这里,我们也对它的初始日期进行了设置。
对齐事件进行监听即可达到目的,并没有代理协议。
- (void)showDate:(UIDatePicker *)datePicker { NSDate *date = datePicker.date; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd"; NSString *outputString = [formatter stringFromDate:date]; _dateField.text = outputString; }
UIImagePickerController
如果不自定义选取照片视图的话,实现照片或者拍照选取功能十分简单,设置
协议就可以快速调用。
UIImagePickerController *imagePC = [[UIImagePickerController alloc] init]; //从照片库中选择图片, 也可以设置相机 imagePC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //允许对图片进行简单的编辑 imagePC.allowsEditing = YES; imagePC.delegate = self; [self presentViewController:imagePC animated:YES completion:nil];
协议方法获取图片并进行设置
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { const NSString *REditedImageIdentifier = @"UIImagePickerControllerEditedImage"; UIImage *image = info[REditedImageIdentifier]; [_imageButton setImage:image forState:UIControlStateNormal]; [picker dismissViewControllerAnimated:YES completion:nil]; }
UIPickerView
在Component较多的时候,PickerView稍微要复杂一些。首先初始化和设置属性
- (void)pickerViewAndMethod { UIPickerView *pickerView = [[UIPickerView alloc] init]; pickerView.delegate = self; pickerView.dataSource = self; //显示选择活动器 pickerView.showsSelectionIndicator = YES; _cityField.inputView = pickerView; _cityField.text = @"北京 - 海淀"; }
实现数据源和代理方法,来刷新自身数据和界面。
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return rComponentCount; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == rProvinceComponent) { return _cityList.count; } else { NSInteger i = [pickerView selectedRowInComponent:rProvinceComponent]; NSArray *arr = _cityList[i][@"city"]; return arr.count; } } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == rProvinceComponent) { NSString *proName = _cityList[row][@"province"]; return proName; } else { NSInteger i = [pickerView selectedRowInComponent:rProvinceComponent]; NSString *cityName = _cityList[i][@"city"][row]; return cityName; } } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { [pickerView reloadComponent:rCityComponent]; NSInteger proIndex = [pickerView selectedRowInComponent:rProvinceComponent]; NSString *proName = _cityList[proIndex][@"province"]; NSInteger cityIndex = [pickerView selectedRowInComponent:rCityComponent]; NSString *cityName = _cityList[proIndex][@"city"][cityIndex]; _cityField.text = [NSString stringWithFormat:@"%@ - %@", proName, cityName]; }
数据源来自于自己写的一个简单的Plist文件。一个数组中包含多个字典。
这样一个使用各种Picker进行设置的功能就完成了。
demo示例点击打开链接
以上为本篇博客全部内容,欢迎指正和交流。转载注明出处~