UIDatePicker设置最大日期 [英] UIDatePicker set Maximum date

查看:60
本文介绍了UIDatePicker设置最大日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using this code to block the user from going over the limit I set:

视图中确实加载了

NSDate *Date=[NSDate date];
[DatePickerForDate setMinimumDate:Date];
[DatePickerForDate setMaximumDate:[Date dateByAddingTimeInterval: 63072000]]; //time interval in seconds

这种方法:

- (IBAction)datePickerChanged:(id)sender{
if ( [DatePickerForDate.date timeIntervalSinceNow ] < 0 ){
    NSDate *Date=[NSDate date];
    DatePickerForDate.date = Date;
}

if ( [DatePickerForDate.date timeIntervalSinceNow ] > 63072000){
    NSDate *Date=[NSDate date];
    DatePickerForDate.date = Date;
}
}

第一部分有效(具有< 0的部分),并返回到当前日期,但是> 63072000有时有效,有时无效.顺便说一下,63072000是大约2年.有什么想法吗?

The first part works (the one with <0), and returns to the current date, but the one > 63072000, sometimes works and sometimes doesn't. By the way 63072000 is about 2 years. Any ideas?

推荐答案

我从现在开始使用UIDatePicker进行了实验,最大日期为一个月:

I experimented using a UIDatePicker with a maximumDate of one month from now:

NSDate* now = [NSDate date] ;
// Get current NSDate without seconds & milliseconds, so that I can better compare the chosen date to the minimum & maximum dates.
NSCalendar* calendar = [NSCalendar currentCalendar] ;
NSDateComponents* nowWithoutSecondsComponents = [calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:now] ;
NSDate* nowWithoutSeconds = [calendar dateFromComponents:nowWithoutSecondsComponents] ;
//  UIDatePicker* picker ;
picker.minimumDate = nowWithoutSeconds ;

NSDateComponents* addOneMonthComponents = [NSDateComponents new] ;
addOneMonthComponents.month = 1 ;
NSDate* oneMonthFromNowWithoutSeconds = [calendar dateByAddingComponents:addOneMonthComponents toDate:nowWithoutSeconds options:0] ;
picker.maximumDate = oneMonthFromNowWithoutSeconds ;

我发现:

  • 第一次尝试选择超出最小和最大范围的日期时,UIDatePicker会在范围内"向后滚动.
  • 如果您立即再次选择超出范围的日期,选择器将不会向后滚动,从而允许您选择超出范围的日期.
  • 如果选择器的选定日期超出范围,则其 date 属性将返回该范围内的最近日期.
  • 当您调用 setDate: setDate:animated:时,如果传递的日期与选择器的 date 返回的日期完全相同属性,选择器将不执行任何操作.
  • The first time you attempt to select a date outside of the minimum and maximum range, the UIDatePicker will scroll itself back "in range".
  • If you immediately select a date out of range again, the Picker will not scroll back, allowing you to select a date out of range.
  • If the Picker's selected date is out of range, its date property will return the nearest date that's in range.
  • When you call setDate: or setDate:animated:, if the date you pass is the exact same date returned by the Picker's date property, the Picker will do nothing.

请牢记这一点,这是一种在选择器的值更改时可以调用的方法,该方法使您无法选择超出范围的日期:

With that in mind, here is a method that you can call when the Picker's value changes that prevents you from ever selecting a date out of range:

- (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 = [picker.date dateByAddingTimeInterval:1] ;
    if ( [picker.date compare:picker.minimumDate] == NSOrderedSame ) {
        NSLog(@"date is at or below the minimum") ;
        picker.date = oneSecondAfterPickersDate ;
    }
    else if ( [picker.date compare:picker.maximumDate] == NSOrderedSame ) {
        NSLog(@"date is at or above the maximum") ;
        picker.date = oneSecondAfterPickersDate ;
    }
}

上面的 if else if 部分几乎相同,但我将它们分开,以便可以看到不同的NSLogs,并进行更好的调试.

The above if and else if sections are nearly identical, but I kept them separate so that I could see the different NSLogs, and also to better debug.

这是GitHub上的有效项目.

这篇关于UIDatePicker设置最大日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆