你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS中的各種Picker

IOS中的各種Picker

編輯:IOS開發綜合
簡述   在應用的一些設置中經常要用到一些Picker來快速幫助用戶選定取值,一般會用到的有UIDatePicker,UIPickerView以及UIImagePickerController。   初始界面     如圖,在搭建好的界面,實現方法來完成各個選擇。首先是日期選擇   UIDatePicker   主要需要設置的屬性有 locale datePickerMode   [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進行設置的功能就完成了。
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved