第五章:第一节 UIDatePicker
Step---1:
UIDatePicker*_datePiceker = [[UIDatePickeralloc] init];
_datePiceker.frame= CGRectMake(10, 10, 300, 100);
/*设置日期选择控件的样式
UIDatePickerModeTime 时间模式
UIDatePickerModeDate 日期模式
UIDatePickerModeDateAndTime 日期时间模式
UIDatePickerModeCountDownTimer 倒计时模式
*/
_datePiceker.datePickerMode= UIDatePickerModeTime;
//设置日期选择控件的地区
NSLocale*_local = [[NSLocalealloc]initWithLocaleIdentifier:@"zh_Hans_CN"];
_datePiceker.locale= _local;
//默认为当天
NSCalendar*_calendar = [NSCalendarcurrentCalendar];
_datePiceker.calendar= _calendar;
//设置时区。
NSTimeZone*_timeZone = [NSTimeZonedefaultTimeZone];
_datePiceker.timeZone= _timeZone;
//日期
NSDate*_date = [NSDatedate];
_datePiceker.date= _date;
//倒计时 先指定模式 UIDatePickerModeCountDownTimer
NSTimeInterval_minuteInterval = 60* 6;
_datePiceker.countDownDuration= _minuteInterval;
//设置最大 和 最小 先指定模式 UIDatePickerModeTime
NSTimeIntervaloneYearInterval = 365* 24* 60* 60;
NSDate*today = [NSDatedate];
NSDate*oneYearFromToday = [today dateByAddingTimeInterval:oneYearInterval];
NSDate*twoYearFromToday = [today dateByAddingTimeInterval:oneYearInterval * 2];
_datePiceker.minimumDate= oneYearFromToday;
_datePiceker.maximumDate= twoYearFromToday;
//监听事件
[_datePiceker addTarget:selfaction:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
[self.viewaddSubview:_datePiceker];
- (void)dateChange:(UIDatePicker*)change{
NSDate*date = change.date;
NSDateFormatter*formatter = [[NSDateFormatteralloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString*dateString = [formatter stringFromDate:date];
NSLog(@"当前日期 %@",dateString);
}