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);
}
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;
//获取列数
//NSInteger number = pickView.numberOfComponents;
pickView.dataSource= self;遵守协议:UIPickerViewDataSource
pickView.delegate= self;遵守协议: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列的当前选中的行号
*/
[self.viewaddSubview:pickView];
//选择器中拨轮的个数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{
return3;
}
//选择器某个拨轮的行数数
-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
return1;
}
//选择器标题
-(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;
}