我试图在日期选择器中设置最大和最小日期,如下所示。
UIDatePicker *picker = [[UIDatePicker alloc] init];
[picker setDatePickerMode:UIDatePickerModeDate];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *currentDt = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:-10];
NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDt options:0];
[comps setYear:10];
NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDt options:0];
[picker setMaximumDate:maxDate];
[picker setMinimumDate:minDate];
最高年份必须比当年提前10年,而最低年度必须比当年低10年
但这种方法不起作用。它显示了一切。
答案 0 :(得分:1)
试试这个,它会起作用
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:10];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-10];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];