Ios 在UIDatePicker上设置时间范围
我有一个Ios 在UIDatePicker上设置时间范围,ios,objective-c,xcode,uidatepicker,Ios,Objective C,Xcode,Uidatepicker,我有一个UIDatePicker,我已经通过编程将它添加到我的代码中,现在我想看看是否可以以某种方式设置时间范围。因此,用户只能选择一个日期,以及介于09:00和17:30之间的时间。理想情况下,任何其他选项都不会出现。有没有办法把它添加到我的UIDatePicker代码中 这是我的密码: CGRect pickerFrame = CGRectMake(0,250,0,0); UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFram
UIDatePicker
,我已经通过编程将它添加到我的代码中,现在我想看看是否可以以某种方式设置时间范围。因此,用户只能选择一个日期,以及介于09:00和17:30之间的时间。理想情况下,任何其他选项都不会出现。有没有办法把它添加到我的UIDatePicker代码中
这是我的密码:
CGRect pickerFrame = CGRectMake(0,250,0,0);
UIDatePicker *myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[myPicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:myPicker];
UIDatePicker
具有minimumDate
和maximumDate
的属性
你应该习惯阅读报纸
通常只需将类名粘贴到google中,第一个结果就是Apple文档。尝试如下:-
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:10];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-10];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[yourdatePicker setMaximumDate:maxDate];
[yourdatePicker setMinimumDate:minDate];
除了设置的范围外,您不能选择其他选项。希望这能起作用:只需设置
minimumDate
和maximumDate
属性。请参阅此答案@Luke。如果答案正确,请记住接受答案。