UIDatePicker:向时间添加约束

时间:2013-07-18 22:18:38

标签: ios uidatepicker datetime-format

有没有办法限制/约束UIDatePicker的时间。

即。如果我希望拣货员只在下午3点到晚上8点之间显示时间,那么之后的任何事情都没有,这可能吗?

代码示例会很好。谢谢!

编辑

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{


    // **EDIT 2**
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMinute:10];
    [comps setHour:5];
    NSDate *minimumTime = [[NSCalendar currentCalendar] dateFromComponents:comps];


    UIDatePicker *pickerView = [[[UIDatePicker alloc] init] initWithFrame:CGRectMake(0, 40, 320, 216)];

    pickerView.datePickerMode = UIDatePickerModeTime;
    [pickerView setMinimumDate:minimumTime];

    [pickerView setMinuteInterval:15];
    [pickerView setTag: kDatePickerTag1];
    //Add picker to action sheet
    [actionSheet addSubview:pickerView];
    NSArray *subviews = [actionSheet subviews];
}

startTimeLabel是一个NSString变量(即下午6:30),应该用作UIPicker的最短时间。

图像: enter image description here

1 个答案:

答案 0 :(得分:2)

使用此代码:

[datePicker setMaximumDate:yourmaxDate];
[datePicker setMinimumDate:yourminDate];

创建NSDate:

NSDate *todaysDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components: NSUIntegerMax fromDate:todaysDate];
[comps setMinute:10];
[comps setHour:5];
[comps setSecond:0];
NSDate *yourminDate = [gregorian dateFromComponents:comps];