显示datepicker 18年前,并locking在iOS中的Ui datepicker

在我的项目中,我需要显示dateselect器18回来,我需要locking80年后,所以我怎么能显示datepicker这些date可以任何一个帮助我在这里find这里我加我的代码我打印在日志中,但我需要显示在我的dateselect器,所以我如何显示

NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDate * currentDate = [NSDate date]; NSDateComponents * comps = [[NSDateComponents alloc] init]; [comps setYear: -18]; NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps setYear: -100]; NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps release]; self.datePickerView.minimumDate = minDate; self.datePickerView.maximumDate = maxDate; NSLog(@"minDate %@", minDate); NSLog(@"maxDate%@", maxDate); 

嗨,首先最小date必须是100年的返回date和最大date必须是18年的返回date,然后将pickerView的date设置date范围之间的date,以便您的最终代码将看起来像

 NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDate * currentDate = [NSDate date]; NSDateComponents * comps = [[NSDateComponents alloc] init]; [comps setYear: -18]; NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps setYear: -100]; NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps release]; self.datePickerView.minimumDate = minDate; self.datePickerView.maximumDate = maxDate; self.datePickerView.date = minDate; 

希望这会对你有所帮助。

这里是视图控制器类的viewDidLoad方法

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDate * currentDate = [NSDate date]; NSDateComponents * comps = [[NSDateComponents alloc] init]; [comps setYear: -18]; NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps setYear: -100]; NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps release]; UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 300)]; [pickerView setMinimumDate:minDate]; [pickerView setMaximumDate:maxDate]; [pickerView setDate:minDate]; [[self view] addSubview:pickerView]; } 

我的输出是

在这里输入图像说明

你能分享你的输出截图吗?