xcode UIDatePicker 设置最大日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14694452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIDatePicker set Maximum date
提问by user2014474
I am using this code to block the user from going over the limit I set:
我正在使用此代码来阻止用户超过我设置的限制:
in view did load:
鉴于确实加载:
NSDate *Date=[NSDate date];
[DatePickerForDate setMinimumDate:Date];
[DatePickerForDate setMaximumDate:[Date dateByAddingTimeInterval: 63072000]]; //time interval in seconds
And this method:
而这个方法:
- (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;
}
}
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?
第一部分有效(<0 的那个),并返回到当前日期,但 > 63072000 的部分有时有效,有时无效。顺便说一下,63072000 大约是 2 年。有任何想法吗?
回答by John Sauer
I experimented using a UIDatePicker with a maximumDate of one month from now:
我尝试使用 UIDatePicker,从现在开始的最大日期为一个月:
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 ;
I found that:
我找到:
- 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:
orsetDate:animated:
, if the date you pass is the exact same date returned by the Picker'sdate
property, the Picker will do nothing.
- 第一次尝试选择最小和最大范围之外的日期时,UIDatePicker 会将自身滚动回“范围内”。
- 如果您立即再次选择超出范围的日期,选择器将不会向后滚动,从而允许您选择超出范围的日期。
- 如果选择器的选定日期超出范围,则其
date
属性将返回范围内最近的日期。 - 当您调用
setDate:
或 时setDate:animated:
,如果您通过的日期与 Picker 的date
属性返回的日期完全相同,则 Picker 将不执行任何操作。
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:
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 ;
}
}
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.
以上if
和else if
部分几乎相同,但我将它们分开,以便我可以看到不同的 NSLogs,也为了更好地调试。
Here's the working projecton GitHub.