// 1. 初始化
UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 200, 375, 100)];
// 2. 设置当地时区(点语法 和 setter,getter都可以)
datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_Hans_CN"];// 可不写
// 3. 设置允许的最大最小日期
NSDate *minDate = [NSDate dateWithTimeIntervalSinceNow:-100*12*30*24*60*60];
NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:100*12*30*24*60*60];
datePicker.minimumDate = minDate;
datePicker.maximumDate = maxDate;
[datePicker setDate:minDate animated:YES];
// 4.输出模式
datePicker.datePickerMode = UIDatePickerModeDate;// 默认设置
// 5. 读取日期
NSDate *date = datePicker.date;
NSLog(@"%@",date);
// 6. addTarget
[datePicker addTarget:self action:@selector(dateAction:) forControlEvents:UIControlEventValueChanged];
- (void)dateAction:(UIDatePicker * )sender{
NSLog(@"%@",sender.date);
}
网友评论