IOS中的各種Picker

來源:互聯網
上載者:User

簡述 在應用的一些設定中經常要用到一些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進行設定的功能就完成了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.