ios - Swift update UILabel with dispatch_async not working -


why doesn't work?

dispatch_async(dispatch_get_main_queue(), {     self.timestringlabel.text = "\(self.timestringselected)"      println(self.timestringlabel.text) }) 

i'm trying update label in swift ui label never changes. keep googling it, can't find responses don't use dispatch_async. doing wrong?

1st edit: mistaken. i'm not printing updated text. text never changes. prints out optional("0") if helps. default value 0 defined in storyboard.

i have tried , without dispatch_async without success. tried adding self.timestringlabel.setneedsdisplay() after updating text, doesn't work.

edit 2: here's complete function + uilabel declaration

@iboutlet weak var timenumberlabel: uilabel!  @ibaction func timenumberbuttonpressed(sender: uibutton) {      println("number selected. tag \(sender.tag)")      dispatch_async(dispatch_get_main_queue()) {          self.timenumberonebutton.selected = false         self.timenumbertwobutton.selected = false         self.timenumberthreebutton.selected = false         self.timenumberfourbutton.selected = false          if sender.tag == 0{             self.timenumberselected = 0         } else if sender.tag == 1 {             self.timenumberselected == 5         } else if sender.tag == 2 {             self.timenumberselected == 10         } else {             self.timenumberselected == 24         }          sender.selected = true          self.timenumberlabel.text = "\(self.timenumberselected)"          self.timenumberlabel.setneedsdisplay()           println(self.timenumberlabel.text)     } } 

the label visible shown in picture. didn't think hard implement, wrong. i'm willing bet it's simple i'm missing.

enter image description here

try adding line

self.timestringlabel.setneedsdisplay() 

(after label change)

because code run asynchronously, interface-updating methods may miss change , not display (especially if time-consuming bit of code occurs before label change). adding code forces interface check , display changes may have missed.

this should used after asynchronous task changes interface, task's running may overlap interface methods, resulting in missed change.

*thanks ios development journal


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -