如何防止DatePicker滚动到过去的日期?

时间:2014-03-15 16:49:10

标签: ios objective-c datepicker

我有一个看起来像这样的DatePicker:

enter image description here

我从DatePicker中选择日期设置通知,显然我不需要过去的日期,我该如何隐藏它们?我尝试了一些我在其他问题中发现但没有工作的东西,看起来像这样:

[super viewDidLoad];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    [self.DatePicker setMinimumDate:minDate];

谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个

[picker setMinimumDate: [NSDate date]];

或尝试此代码

 NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
 NSDate *currentDate = [NSDate date];
 NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
 [comps setYear:30];
 NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
 [comps setYear:-30];
 NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

 [datePicker setMaximumDate:maxDate];
 [datePicker setMinimumDate:minDate];

它有希望对你有用。