2013-07-18 109 views
1

是否有方法限制/約束UIDatePicker的時間。UIDatePicker:將約束添加到時間

即,如果我希望選取器僅在下午3點到8點之間顯示時間,那麼在此之後沒有任何事情,那可能嗎?

代碼示例會很好。謝謝!

EDIT

- (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是應作爲對UIPicker最小時間一個NSString變量(即6:30 PM)。

圖片: enter image description here

回答

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]; 
+0

這篇代碼對你的工作? –

+0

謝謝。我試過上面的代碼,它不工作。我已更新該帖子。 – user1107173

+0

我仍然可以在拾取器中看到整整24小時。 – user1107173