【问题标题】:How to disable previous time in UIDatePicker in iOS如何在 iOS 的 UIDatePicker 中禁用以前的时间
【发布时间】:2012-09-19 16:26:32
【问题描述】:

我想从 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;

`

在此先感谢如果有任何方法可以执行此操作,请建议我。

【问题讨论】:

  • 时间选择器?你的意思是?你在说什么课?

标签: iphone objective-c ios datepicker


【解决方案1】:

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

   [datePicker setMinimumDate:[NSDate date]];

这里

[NSDate date]

给出当前日期

【讨论】:

  • 这个东西只用于显示日期,它不能用于显示时间。正如我在 Apple 文档中看到的,MinimumDate 属性不适用于 UIDatePickerModeCountDownTimer。我正在使用这样的日期选择器:timePicker.datePickerMode = UIDatePickerModeCountDownTimer;
【解决方案2】:

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

timePicker.date = [NSDate date];

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

【讨论】:

    【解决方案3】:

    通过这种方法,如果用户选择了过去的时间,您可以向用户提供警报。条件是您已经将支票的最小日期设置为当天。

    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)
    
        }
    }
    

    【讨论】:

      【解决方案4】:

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

      My_Pickername.minimumDate = NSDate()

      【讨论】:

        【解决方案5】:

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

        斯威夫特 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)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-04-10
          • 2013-04-11
          • 1970-01-01
          • 1970-01-01
          • 2011-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多