2012-09-27 56 views
3

我想禁用之前的時間,即從datePicker,之前的當前時間。在日期選擇器中,我們可以使用最小日期做到這一點。我們可以如何在時間選擇器上做同樣的事情。如何禁用iOS中的UIDatePicker中的以前的時間

這裏是Code.`

timePicker = [[UIDatePicker alloc] init]; 
    [self.view addSubview:timePicker]; 
    float tempHeight = timePicker.frame.size.height; 
    timePicker.frame = CGRectMake(0,200 ,self.view.frame.size.width , tempHeight); 
    timePicker.hidden = YES; 
    timePickerFormatter= [[NSDateFormatter alloc]init ]; 
    [timePickerFormatter setDateFormat:@"HH:mm"]; 

    **timePicker.datePickerMode = UIDatePickerModeCountDownTimer;** 
    timePicker.minuteInterval=15; 

`

在此先感謝請建議我,如果任何方式來執行此。

+1

時間選擇器?你什麼意思?你在說什麼課? – geraldWilliam

回答

1

所以你想從當前時間禁用前一次。

timePicker.date = [NSDate date]; 

這將時間設置爲當前時間,但我不認爲您可以禁用以前的時間。

2

在默認的UIDatePicker最小日期可以設置和日期屬性都有日期和時間值,你可以在此設置本身

[datePicker setMinimumDate:[NSDate date]]; 

這裏

[NSDate date] 

給當前日期

+1

這個東西是爲了顯示日期而不是顯示時間的工作。當我讀到蘋果文檔MinimumDate屬性不適用於UIDatePickerModeCountDownTimer。我使用這樣的日期選擇器:timePicker.datePickerMode = UIDatePickerModeCountDownTimer; – Sawant

+0

你是通過筆尖設置的嗎? –

0

Swift:將這一行添加到您的選取器功能中。僅允許將來的時間。

My_Pickername.minimumDate = NSDate的()

1

通過這種方法,如果他/她選擇的時間過去時,你可以提供警報用戶。條件是您已經將最低日期的支票設置爲當天。

func handleTimePicker(sender: UIDatePicker) { 

    let timeFormatterHour = NSDateFormatter() 
    let timeFormatterMinutes = NSDateFormatter() 
    timeFormatterHour.dateFormat = "hh" 
    timeFormatterMinutes.dateFormat = "mm" 
    let InputTimeHourString = timeFormatterHour.stringFromDate(sender.date) 
    let InputTimeMinutesString = timeFormatterMinutes.stringFromDate(sender.date) 
    let InputTimeHourInt = Int(InputTimeHourString)! 
    let InputTimeMinutesInt = Int(InputTimeMinutesString)! 
    let calculatedInputMinutes = (InputTimeHourInt * 60) + InputTimeMinutesInt 

    let TodayTime = NSDate() 
    let TodayTimetimeFormatterHour = NSDateFormatter() 
    let TodayTimetimeFormatterMinutes = NSDateFormatter() 
    TodayTimetimeFormatterHour.dateFormat = "hh" 
    TodayTimetimeFormatterMinutes.dateFormat = "mm" 
    let TodayTimeInputTimeHourString = TodayTimetimeFormatterHour.stringFromDate(TodayTime) 
    let TodayTimeInputTimeMinutesString = TodayTimetimeFormatterMinutes.stringFromDate(TodayTime) 
    let TodayTimeInputTimeHourInt = Int(TodayTimeInputTimeHourString)! 
    let TodayTimeInputTimeMinutesInt = Int(TodayTimeInputTimeMinutesString)! 
    let TodayTimecalculatedInputMinutes = (TodayTimeInputTimeHourInt * 60) + TodayTimeInputTimeMinutesInt 

    if TodayTimecalculatedInputMinutes - calculatedInputMinutes < 0 
    { 
     let shownDateFormatter = NSDateFormatter() 
     shownDateFormatter.dateFormat = "hh:mm a" 
     self.txtTimeEvent.text = shownDateFormatter.stringFromDate(sender.date) 
    } 
    else 
    { 
     let alertController = UIAlertController(title: "Time Selection Problem", message: 
      "You cannot select the Past time", preferredStyle: UIAlertControllerStyle.Alert) 
     alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil)) 
     self.presentViewController(alertController, animated: true, completion: nil) 
     let shownTime = NSDate() 
     let shownTimeFormatter = NSDateFormatter() 
     shownTimeFormatter.dateFormat = "hh:mm a" 
     self.txtTimeEvent.text = shownTimeFormatter.stringFromDate(shownTime) 

    } 
} 
相關問題