Step---1:
?UIDatePicker*_datePiceker = [[UIDatePickeralloc]?init];
??? _datePiceker.frame=?CGRectMake(10,?10,?300,?100);
?/*設(shè)置日期選擇控件的樣式
???? UIDatePickerModeTime 時間模式
???? UIDatePickerModeDate 日期模式
???? UIDatePickerModeDateAndTime 日期時間模式
???? UIDatePickerModeCountDownTimer 倒計時模式
???? */
??? _datePiceker.datePickerMode=?UIDatePickerModeTime;
?//設(shè)置日期選擇控件的地區(qū)
?NSLocale*_local =? [[NSLocalealloc]initWithLocaleIdentifier:@"zh_Hans_CN"];
??? _datePiceker.locale= _local;
?//默認(rèn)為當(dāng)天
?NSCalendar*_calendar = [NSCalendarcurrentCalendar];
??? _datePiceker.calendar= _calendar;
?//設(shè)置時區(qū)。
?NSTimeZone*_timeZone = [NSTimeZonedefaultTimeZone];
??? _datePiceker.timeZone= _timeZone;
?//日期
?NSDate*_date = [NSDatedate];
??? _datePiceker.date= _date;
?//倒計時 先指定模式 UIDatePickerModeCountDownTimer
?NSTimeInterval_minuteInterval =?60*?6;
??? _datePiceker.countDownDuration= _minuteInterval;
?//設(shè)置最大 和 最小 先指定模式 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;
?//監(jiān)聽事件
[_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(@"當(dāng)前日期 %@",dateString);
}
Step—2:UIPickerView
?UIPickerView*pickView = [[UIPickerViewalloc]?init];
??? pickView.frame=?CGRectMake(20,self.view.frame.size.height-400,self.view.frame.size.width-40,?400);
?//是否顯示選擇的行
??? pickView.showsSelectionIndicator=YES;
?//獲取列數(shù)
?//NSInteger number = pickView.numberOfComponents;
??? pickView.dataSource=?self;遵守協(xié)議:UIPickerViewDataSource?
??? pickView.delegate=?self;遵守協(xié)議:UIPickerViewDelegate?
?/*常用方法
???? - (NSInteger)numberOfRowsInComponent:(NSInteger)component;
???? - (CGSize)rowSizeForComponent:(NSInteger)component;
???? - (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
???? - (void)reloadAllComponents;//刷新所有列
???? - (void)reloadComponent:(NSInteger)component;//刷新某一列
???? - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;//選中第component列的第row行
???? - (NSInteger)selectedRowInComponent:(NSInteger)component;獲得第component列的當(dāng)前選中的行號
???? */
??? [self.viewaddSubview:pickView];
//選擇器中撥輪的個數(shù)
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
?return3;
}
//選擇器某個撥輪的行數(shù)數(shù)
-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
?return1;
}
//選擇器標(biāo)題
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
?return@"---";
}
//被選擇的行
-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
}
//寬
- (CGFloat)pickerView:(UIPickerView*)pickerView widthForComponent:(NSInteger)component{
?returnself.view.frame.size.width-80;
}
//高
- (CGFloat)pickerView:(UIPickerView*)pickerView rowHeightForComponent:(NSInteger)component{
?return60;
}