2013-02-05 137 views
0

可能重复:
UIDatePicker set Maximum dateUIPicker设置日期范围

我使用此代码,不过去我设置的限制阻止用户:鉴于没有负载

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语句中的代码永远不会被调用。为什么?

+0

尝试打印您在某些NSLogs中检索的日期,看看您是否可以确定发生了什么,为什么它可能无法进行比较。 –

+0

当我打印日期时,根据格式,我得到UIDatePicker上显示的日期(yyyy ...) – user2014474

+0

并且最小和最大日期是市场。日期超出范围灰色 – user2014474

回答