Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UIDatePicker上设置时间范围_Ios_Objective C_Xcode_Uidatepicker - Fatal编程技术网

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。如果答案正确,请记住接受答案。