簡述 在應用的一些設定中經常要用到一些Picker來快速協助使用者選定取值,一般會用到的有UIDatePicker,UIPickerView以及UIImagePickerController。 初始介面 ,在搭建好的介面,實現方法來完成各個選擇。首先是日期選擇 UIDatePicker 主要需要設定的屬性有localedatePickerMode [cpp] 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; 在這裡,我們也對它的初始日期進行了設定。 對齊事件進行監聽即可達到目的,並沒有代理協議。 [cpp] - (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 如果不自訂選取照片視圖的話,實現照片或者拍照選取功能十分簡單,設定協議就可以快速調用。 [cpp] UIImagePickerController *imagePC = [[UIImagePickerController alloc] init]; //從照片庫中選擇圖片, 也可以設定相機 imagePC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //允許對圖片進行簡單的編輯 imagePC.allowsEditing = YES; imagePC.delegate = self; [self presentViewController:imagePC animated:YES completion:nil]; 協議方法擷取圖片並進行設定 [cpp] - (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稍微要複雜一些。首先初始化和設定屬性 [cpp] - (void)pickerViewAndMethod { UIPickerView *pickerView = [[UIPickerView alloc] init]; pickerView.delegate = self; pickerView.dataSource = self; //顯示選擇活動器 pickerView.showsSelectionIndicator = YES; _cityField.inputView = pickerView; _cityField.text = @"北京 - 海澱"; } 實現資料來源和代理方法,來重新整理自身資料和介面。 [cpp] - (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進行設定的功能就完成了。