dimanche 10 mai 2015

Count down from date picker with Swift

I could create a count down from a number, in this case 5 and it works well. But how could I put in the label the date of the date picker and count down from that?

1- I think I could grab the selected date in the date picker.

let wakeUpTime = datePicker.date

2- Next step is to put that in the "countingLabel" and this is my next problem I could not solve. Of course, this does not work:

countingLabel.text = String(wakeUpTime)

So, my question is: how to put the date of the date picker in the label and count down from that?

This is the full code:

var timer = NSTimer()
var counter = 5

@IBOutlet var countingLabel: UILabel!
@IBOutlet weak var datePicker: UIDatePicker!

@IBAction func start(sender: AnyObject) {
    // grab the selected date from the date picker
    let wakeUpTime = datePicker.date

    // count down
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}

func updateCounter() {
    countingLabel.text = String(counter--)
}

Aucun commentaire:

Enregistrer un commentaire