UIDate Picker valuechanged does not update the first spin, but does every spin after.. iOS

前端 未结 8 2084
野的像风
野的像风 2021-02-12 14:37

I have a UIDate Picker embedded in a static TableViewCell and at the moment I disabled most of the code except the code responsible for the date picker.

I\'m using t

相关标签:
8条回答
  • 2021-02-12 15:21

    I don't know if I got your problem, but Date picker works using the target-action pattern and it triggers this mechanism when you change a value.
    So if the problem is the very first value shown, you should initialize your variable taking it as the "default" value.

    0 讨论(0)
  • 2021-02-12 15:32

    Actually, all you need is to not set datePicker.countDownDuration in viewDidLoad but add it to viewDidAppear, or later.

    0 讨论(0)
  • 2021-02-12 15:33

    Similar problem as above, I used

    DispatchQueue.main.asyncAfter(deadline: .now()) {
       self.datePicker.countDownDuration = 60
    }
    

    to put it on the next runloop. Seems a viable workaround.

    0 讨论(0)
  • 2021-02-12 15:34

    Time ago I found another problem that somehow resembles this one. It was about presenting a view controller from within the tableView:didSelectRowAtIndexPath: and the problem was that it was taking lots of time for the new controller to actually show up. One of the workarounds turned out to be using dispatch_async on the main queue. The same worked for me in this case.

    In my viewDidLoad, I asynchronously dispatched the picker setup code on the main queue and, in my case, it started to respond to the valueChanged event even on the first pick:

    DispatchQueue.main.async {
        self.pickerView.datePickerMode = .countDownTimer
        self.pickerView.minuteInterval = 5
        self.pickerView.countDownDuration = 15
    }
    
    0 讨论(0)
  • 2021-02-12 15:36

    I think you should try implement this delegate instead

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    
    0 讨论(0)
  • 2021-02-12 15:37

    It seems to be something to do with the animated parameter. You just need this line:

    datePicker.setDate(date, animated: true)
    
    0 讨论(0)
提交回复
热议问题