ios - Label not showing value of timer in Swift -
i have timer loop executes basic countdown , prints value console. i'm trying have value set text value of label. though xcode console shows correct countdown of timer value, label in application still shows 0. ideas why happening? here relevant code:
import uikit class gameviewcontroller: uiviewcontroller { @iboutlet weak var timerlabel: uilabel! var timercount = 7 var timerrunning = false var timer = nstimer() override func viewdidload() { super.viewdidload() self.timercount = 7 self.timer = nstimer.scheduledtimerwithtimeinterval(1, target: self, selector: selector("counting"), userinfo: nil, repeats: true) } func counting(){ timercount = 7 { println(timercount) timerrunning = true --timercount timerlabel.text = "\(timercount)" println(timercount) } while timercount > 0 }
the method counting() wrong.
every second launching counting method , within method have loop updates timerlabel.text, ui not updated until counting() finishes...that's why showing 0. need decrease counting every second , update label.
i think need:
func counting(){ if timercount == 0 { timercount = 7 // or self.timer.invalidate() in case want stop } else { timercount--; timerlabel.text = "\(timercount)" println(timercount) } } hope helps
Comments
Post a Comment