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 the Date Picker as a Count Down Timer
So this is kind of all, except some usual outlets and vars:
override func viewDidLoad() {
super.viewDidLoad()
// tfName.delegate = self
// tfDescription.delegate = self
//
datePicker.countDownDuration = 60
// pickerValueChanged(self)
}
@IBAction func pickerValueChanged(sender: AnyObject) {
seconds = Int(datePicker.countDownDuration)
hours = seconds / 3600
if hours == 0{
minutes = seconds / 60
} else{
minutes = seconds % 3600 / 60
}
labelDuration.text = "\(hours) hours \(minutes) minutes"
}
The first time I change the value of the Picker, the value changed function does not fire at all, but the spins after it does without failing.
I tried to call it in viewDidLoad
, with pickerValueChanged(self)
and that works, but does not solve the problem. Also I commented out every line of code in the pickerValueChanged
function and viewDidLoad
but it still did not fire the first spin..
Thank you for your time!
Update: Added pictures
After the first spin, pickerValueChanged does not get called:
From the second spin and beyond, the event gets called and the label is updated:
Tried:
Executing:
datePicker.setDate(NSDate(), animated: true
in viewDidLoad()
solves the problem that the event doesn't get called at the first spin, but this sets the initial state of the Date Picker to the current time. Since I'm using this control to set a duration to a event, I want to let it start at 0 hour and 1 minute.
This can be done with datePicker.countDownDuration = 60
but after this, the main problem gets back again.
So a solution could be setting the DatePicker with a NSDate of 1 minute, but how to do this?
Solution:
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)