UIPicker设置日期范围

时间:2013-02-05 16:54:09

标签: ios xcode

  

可能重复:
  UIDatePicker set Maximum date

我正在使用此代码阻止用户超出我设置的限制:

在视图中加载:

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语句中的代码。为什么呢?

1 个答案:

答案 0 :(得分:0)

你不想完成our discussion over here?

首先,确保在DatePickerForDate的值发生变化时调用datePickerChanged