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

I came up the following solution to initialize the datePicker component (in countdown timer mode) with 0 hours and 1 minute and it responds directly at the first spin. Since this appears to be a bug and is very frustrating if you want a textlabel to update its value when the datePicker is changed and it does not at the first go:

var dateComp : NSDateComponents = NSDateComponents()
dateComp.hour = 0
dateComp.minute = 1
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calendar : NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
var date : NSDate = calendar.dateFromComponents(dateComp)!

datePicker.setDate(date, animated: true)

The timeZone component is not really needed for this to work for my implementation.


Here's @Henk-Martijn's solution updated & simplified for Swift 3:

let calendar = Calendar(identifier: .gregorian)
let date = DateComponents(calendar: calendar, hour: 1, minute: 30).date!
datePicker.setDate(date, animated: true)

Use the above code instead of something like:

datePicker.countDownDuration = 5_400

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.

Tags:

Ios

Datepicker