显示8年前的datepicker并在ios中锁定ui datepicker

问题描述:

在我的项目中,我需要显示日期选择器18,我需要锁定80年后,所以如何在datepicker中显示这些日期可以帮助我找到这里
这里添加代码我的代码我打印在日志中,但我需要在我的日期选择器上显示,以便我如何显示

In my project i need to show the date picker 18 back and i need to lock the 80 years backs so how can i show those date in datepicker can any one help me to find here here am adding code my code i printed in log but i need to display on my datepicker so how can i display

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);


首先,最短的约会日期必须为100年日期和最长日期必须是18年后的日期,然后将pickerView的日期设置为日期范围之间的日期,以便您的最终代码看起来像

Hi first of all minimum date must be 100 year back date and max date must be 18 year back date and then set pickerView's date to a date between range of dates so your final code will be looks like

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;

希望这会对你有所帮助。

Hopefully this will be helpful for you.