UIPickerView 类似于UITableView和UICollectionView
首先实现<UIPickerViewDelegate,UIPickerViewDataSource>
// 返回pickerView有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 3;}
// 返回第component列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return 2;}
#pragma mark - 代理
// 返回第component列的每一行的行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return 80.0;}
// 返回第component列第row行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return @"a";}
//NSAttributedString富文本属性: 可以描述文字大小和颜色
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0); // attributed title is favored if both methods are implemented
// 总结:如果同时实现返回字符串和view的方法,返回UIView的优先级比较高
// 返回第component列第row行的View
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)]; v.backgroundColor = [UIColor redColor]; return v;}
// 选中第component第row的时候调用// __func__: 返回当前方法在哪个类里面调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ NSLog(@"%s---%ld-%ld",__func__,component,row);
}
使用UIPickerView时一般需要自定义键盘
#pragma mark - UITextFieldDelegate
// 是否允许开始编辑
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField//{// return NO;//}
// 是否允许结束编辑
//- (BOOL)textFieldShouldEndEditing:(UITextField *)textField//{// return NO;//}
// 是否允许用户输入文字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return NO;
}
// 文本框开始编辑的时候调用
- (void)textFieldDidBeginEditing:(UITextField *)textField{
// 给生日文本框赋值
[self dateChange:_datePicker];
}
- (void)viewDidLoad { [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. _birthdayField.delegate = self;
// 自定义生日键盘
[self setUpBirthdayKeyboard];
}
// 自定义生日键盘
- (void)setUpBirthdayKeyboard{
// 创建UIDatePicker // 注意:UIDatePicker有默认的尺寸,可以不用设置frame UIDatePicker *picker = [[UIDatePicker alloc] init];
_datePicker = picker;
// 设置地区 zh:中国
picker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
// 设置日期的模式
picker.datePickerMode = UIDatePickerModeDate;
// 监听UIDatePicker的滚动
[picker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
_birthdayField.inputView = picker;
}
// 当UIDatePicker滚动的时候调用
// 给生日文本框赋值
- (void)dateChange:(UIDatePicker *)datePicker{
NSLog(@"%@",datePicker.date);
// 日期转换字符串
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
NSString *dateStr = [fmt stringFromDate:datePicker.date];
_birthdayField.text = dateStr;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}