一、简介
1.继承关系
UIDatePicker继承自UIControl,它和UIPickerView界面有点一样,但是没有直接的联系。
2.使用场景
一般配合UITextField使用,用于选择日期。
3.基本概念
地区:设置了地区,就使用那个地区的语言显示日期
日历:用于处理时间相关问题
时区:地理名称,区域时间
4. 界面
二、常见属性
// 使用地区
@property (nonatomic, retain) NSLocale *locale;
// 日历
@property (nonatomic, copy) NSCalendar *calendar;
// 时区
@property (nonatomic, retain) NSTimeZone *timeZone;
// 时间
@property (nonatomic, retain) NSDate *date;
// 最小时间
@property (nonatomic, retain) NSDate *minimumDate;
// 最大时间
@property (nonatomic, retain) NSDate *maximumDate;
三、常见方法
//设置UIDatePicker所显示的选中时间
- (void)setDate:(NSDate *)date animated:(BOOL)animated;
四、使用方法
UITextField和UIDatePicker的配合使用
//自定义生日键盘
- (void)setUpBirthdayKeyboard {
self.dataPicker = [[UIDatePicker alloc] init];
//设置dataPicker显示类型
self.dataPicker.datePickerMode = UIDatePickerModeDate;
//设置文本框在编辑时弹出dataPicker
self.birthdayTextField.inputView = self.dataPicker;
//监听dataPicker的滚动
[self.dataPicker addTarget:self action:@selector(Changed:) forControlEvents:UIControlEventValueChanged];
}
//dataPicker的滚动事件
- (void)Changed:(UIDatePicker *)dataPicker {
//日期格式对象
NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
//设置日期格式对象的日期格式
dateFormatter.dateFormat = @"yyyy-MM-dd";
//日期转字符串并赋值给birthdayTextField
self.birthdayTextField.text = [dateFormatter stringFromDate:dataPicker.date];
}
#pragma mark - <UITextFieldDelegate>
//是否允许更改字符串;设置UITextField有光标闪动,但不能输入
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return NO;
}