可能重复:
UIDatePicker 设置最大日期
我正在使用此代码来阻止用户超过我设置的限制:
鉴于确实加载:
NSDate *Date=[NSDate date];
[DatePickerForDate setMinimumDate:Date];
[DatePickerForDate setMaximumDate:[Date dateByAddingTimeInterval: 63072000]]; //time interval in seconds
而这个方法:
- (IBAction)datePickerChanged:(id)sender{
// When `setDate:` is called, if the passed date argument exactly matches the Picker's date property's value, the Picker will do nothing. So, offset the passed date argument by one second, ensuring the Picker scrolls every time.
NSDate* oneSecondAfterPickersDate = [DatePickerForDate.date dateByAddingTimeInterval:1] ;
if ( [DatePickerForDate.date compare:DatePickerForDate.minimumDate] == NSOrderedSame ) {
NSLog(@"date is at or below the minimum") ;
DatePickerForDate.date = oneSecondAfterPickersDate ;
}
else if ( [DatePickerForDate.date compare:DatePickerForDate.maximumDate] == NSOrderedSame ) {
NSLog(@"date is at or above the maximum") ;
DatePickerForDate.date = oneSecondAfterPickersDate ;
}
}
if 语句中的代码永远不会被调用。为什么?