更改UILabel文本有延迟,但为什么? [英] Changing UILabel text has delay, but why?
问题描述
在我的应用中,我想设置 UILabel
的文本。该文本来自JSON对象。我将 UILabel
添加到我的故事板,设置 IBOutlet
并调用我的async-method来获取我的JSON对象。
在响应方法中,我设置了 UILabel
的文本。但文本更改需要几秒钟。
当响应响起时,我将其打印到控制台。我可以看到,延迟不是来自异步方法。
响应来了,我可以在控制台中看到它。等待 UIlabel
更改的几秒钟。
我不明白这种行为,有没有一个技巧可以立即刷新 UIlabel
?
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 call my async-method to get my JSON-object.
In the response-method I set the text of the UILabel
. But the text change needs some seconds.
When the response comes I print it to the console. There I can see, that the delay doesnt comes from the async-method.
The response comes, I can see it in the console. Wait some seconds than the UIlabel
changes.
I dont understand this behaviour, is there a trick to refresh the UIlabel
instantly?
一些代码:
@IBOutlet weak var label_news: UILabel!;
override func viewDidLoad() {
super.viewDidLoad()
self.label_news.text = "CHANGE";
rcall.GetNews_GET_NewsResponse_0(self.NewsResponseHandler);
}
func NewsResponseHandler(resp:NewsResponse!){
self.label_news.text = resp.NewsText;
println(resp.NewsText);
}
很抱歉,如果这是一个初学者问题,swift和故事板对我来说都是全新的。
Sorry if this is a beginner question, swift and storyboards are totally new for me.
最好的问候
推荐答案
就像Rob在评论中所说的那样,所有UI更改都需要在主线程上完成。我还没有在Swift中实现它,但Objective-C和我假设的Swift在下面......
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
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;
}
这篇关于更改UILabel文本有延迟,但为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!