如何从iOS中的非UI线程更新UI标签 [英] How to update a UI label from a non UI thread in iOS
本文介绍了如何从iOS中的非UI线程更新UI标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是iOS开发的新手,我有简单的目标-c类MoneyTimer.m用于运行计时器,从那里我想用更改的计时器值更新UI标签。
我想知道如何从非UI线程访问UI元素?我正在使用Xcode 4.2和故事板。
I am new to iOS development, I have plain objective -c class "MoneyTimer.m" for running timer, from there i want to update the an UI label with the changing value of timer. I want to Know how to access the UI element from non UI thread ? I am Using Xcode 4.2 and storyboarding.
在blackberry中,只需获取事件锁即可从非UI线程更新UI。
In blackberry simply by getting the event lock one can update the UI's from non UI thread.
//this the code from MyTimerClass
{...
if(nsTimerUp == nil){
nsTimerUp = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(countUpH) userInfo:nil repeats: YES];
...}
(void) countUpH {
sumUp = sumUp + rateInSecH;
**//from here i want to update the UI label **
...
}
推荐答案
这是最快捷,最简单的方法:
This is the quickest and simplest way to do it is:
- (void) countUpH{
sumUp = sumUp + rateInSecH;
//Accessing UI Thread
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//Do any updates to your label here
yourLabel.text = newText;
}];
}
如果你这样做,你不必切换到另一个方法。
If you do it this way you don't have to switch to a different method.
希望这会有所帮助。
Sam
这篇关于如何从iOS中的非UI线程更新UI标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文