在我的项目中,我需要显示 18 年前的日期选择器,并且需要锁定 80 年前的日期,所以我如何在日期选择器中显示这些日期,任何人都可以帮助我在这里找到
这里添加代码我在日志中打印的代码但我需要在我的日期选择器上显示所以我如何显示
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 的日期设置为日期范围之间的日期,这样您的最终代码将如下所示
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;
希望这对您有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)