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 *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]; }
- (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]; }