3

我想禁用以前的时间,即在 datePicker 的当前时间之前,如在日期选择器中我们可以使用最小日期来执行此操作。我们如何对时间选择器执行相同操作。

这是代码。

  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;

`

提前致谢如果有任何方法可以执行此操作,请建议我。

4

5 回答 5

4

在默认的 UIDatePicker 中可以设置最小日期,并且 date 属性同时具有日期和时间值,您可以在其中自行设置

   [datePicker setMinimumDate:[NSDate date]];

这里

[NSDate date]

给出当前日期

于 2012-09-27T19:58:30.027 回答
1

所以你想从当前时间禁用以前的时间。

timePicker.date = [NSDate date];

这会将时间设置为当前时间,但我认为您不能禁用上一个时间。

于 2012-09-27T19:55:53.303 回答
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)

    }
}
于 2016-03-18T08:39:46.310 回答
0

您在两年前发布了您的问题,但这可能会帮助任何将面临这个问题的人,我今天遇到了同样的问题,这是解决方案:
它并没有真正禁用以前的日期,但是如果用户选择了以前的日期,则上面函数将显示警报并将 UIDatePicker 重置为当前日期:

Swift 4:

@IBAction func dateChanged(_ sender: Any) {
    let dtp = sender as! UIDatePicker
    if Date() <= dtp.date {
        print ("correct date")
    } else {
        dtp.date = Date()
        let alert = UIAlertController(title: "Error", message: "You have to choose a correct date", preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .default, handler: nil)
        alert.addAction(action)
        self.present(alert, animated: true, completion: nil)
    }
}
于 2019-05-01T19:33:50.457 回答
0

Swift:在您的 Picker 函数中添加这一行。它只允许未来的时间。

My_Pickername.minimumDate = NSDate()

于 2015-07-21T06:20:08.857 回答