iOS Swift Update text label after 1 second -


trying update label after 1 second. using sleep function app loading , not updating text field on fly.

code is:

override func viewdidappear(animated: bool) {     begincountdown() }  func begincountdown() {      var = 5; >= 0; i-- {          println("time until launch \(i)")          var county:string = "\(i)"          countdownlabel.text = county         sleep(1)     }  } 

the outlet correct, know i'm missing something. thanks

you shouldn't use sleep() function suspend main thread , cause app become non-responsive. nstimer 1 way achieve this. dispatch function @ specified time in future.

for example -

var countdown=0 var mytimer: nstimer? = nil  override func viewdidappear(animated: bool) {          countdown=5     mytimer = nstimer(timeinterval: 1.0, target: self, selector:"countdowntick", userinfo: nil, repeats: true)     countdownlabel.text = "\(countdown)" }  func countdowntick() {     countdown--      if (countdown == 0) {        mytimer!.invalidate()        mytimer=nil     }      countdownlabel.text = "\(countdown)" } 

Comments