Changing UILabel text has delay, but why?

后端 未结 2 1979
囚心锁ツ
囚心锁ツ 2021-01-22 15:08

in my app I want to set the text of an UILabel. The text comes from a JSON-object. I add my UILabel to my storyboard, set the IBOutlet and

相关标签:
2条回答
  • 2021-01-22 15:48

    c_rath's answer is correct. In swift 3 the syntax was changed (yet again) to

    DispatchQueue.main.async {
        self. label_news?.text = resp.NewsText
    }
    
    0 讨论(0)
  • 2021-01-22 16:09

    Like what Rob stated in the comment, all UI changes need to be done on the main thread. I haven't implemented it in Swift yet, but the Objective-C and what I'm assuming would be the Swift is below...

    Objective-C

    dispatch_async(dispatch_get_main_queue(), ^{
        self.label_news.text = resp.NewsText;
    });
    

    Swift:

    dispatch_async(dispatch_get_main_queue()) {
        self.label_news.text = resp.NewsText;
    }
    
    0 讨论(0)
提交回复
热议问题