UIDatePicker bug? UIControlEventValueChanged after hitting minimum internal

后端 未结 7 658
星月不相逢
星月不相逢 2020-12-08 05:21

I\'ve run into a weird effect that sure looks like a bug in iOS7 -- but often in the past, when I have thought I found a bug in Apple\'s APIs, it has turned out to be my own

相关标签:
7条回答
  • 2020-12-08 05:29

    Swift 4

    @IBOutlet weak var fromPickerView: UIDatePicker!
    
    
    @objc func toPickerViewDateChanged() {
            fromPickerView.minimumDate = toPickerView.date
        }
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
    
    toPickerView.backgroundColor = UIColor.white
        toPickerView.tintColor = .black
        toPickerView.maximumDate = Date()
        toPickerView.addTarget(self, action: 
        #selector(toPickerViewDateChanged), for: UIControlEvents.valueChanged)
    
    0 讨论(0)
  • 2020-12-08 05:31

    Since no answer has been accepted yet. The simplest solution that worked for me in swift 3 is to simply do

    datePicker.countDownDuration = seconds

    in viewDidAppear instead of viewDidLoad

    0 讨论(0)
  • 2020-12-08 05:33

    If someone still having problems with datepicker... I'm using Swift / iOS 8 / Xcode 6.3

    To solve the problem you should not use picker.countDownDuration = NSTimeInterval. Use .setDate(NSDate, animated: true).

    it works direct on viewDidLoad(), don't need to use queues

    The complete snippet:

    override func viewDidLoad() {
        super.viewDidLoad()
        picker.setDate(setDateFromSeconds(seconds), animated: true)
    }
    
    func setDateFromSeconds(seconds: Double) -> (NSDate) {
        let intSeconds = Int(seconds)
        let minutes = (intSeconds / 60) % 60
        let hours = intSeconds / 3600
        let dateString = NSString(format: "%0.2d:%0.2d", hours, minutes)
    
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "hh:mm"
        return dateFormatter.dateFromString(dateString as String) as NSDate!
    }
    
    0 讨论(0)
  • 2020-12-08 05:37

    If someone still having problems with datepicker... I'm using Swift / iOS 8 / Xcode 6.3

    So solve the problem you should no use

    picker.countDownDuration = NSTimeInterval
    

    instead, use setDate

    picker.setDate(NSDate, animated: true)
    

    it works direct on viewDidLoad(), don't need to use `queues

    0 讨论(0)
  • 2020-12-08 05:40

    I was still hitting this issue in 7.1 but adding the following to the UIControlEventValueChanged handler fixed it for me.

    // Value Changed Event is not firing if minimum value hit
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [self.myDatePicker setCountDownDuration: self.myDatePicker.countDownDuration];
    });
    
    0 讨论(0)
  • 2020-12-08 05:41

    I can also confirm that the iOS 7.0.3 UIDatePicker has a bug in it when used in UIDatePickerModeCountDownTimer mode. The picker does not fire the target-action associated with the UIControlEventValueChanged event the first time the user changes the value by scrolling the wheels. It works fine for subsequent changes.

    Below is an efficient workaround. Simply enclose the code that sets the initial value of the countDownDuration in a dispatch block to the main loop. Your target-action method will fire every time the wheels are rotated to a new value. This approach has almost no overhead and works quite well on an iPhone 4 and iPad 4.

    dispatch_async(dispatch_get_main_queue(), ^{
        self.myDatePicker.countDownDuration = (NSTimeInterval) aNewDuration ;
    });
    

    Swift 5:

    DispatchQueue.main.async {
        self.myDatePicker.countDownDuration = aNewDuration
    }
    
    0 讨论(0)
提交回复
热议问题