我是 iOS 开发新手,我有简单的目标 -c 类“MoneyTimer.m”来运行计时器,从那里我想用计时器的变化值更新 UI 标签。
我想知道如何从非 UI 线程访问 UI 元素?我正在使用 Xcode 4.2 和故事板。
在黑莓中,只需获取事件锁,就可以从非 UI 线程更新 UI。
//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 **
...
}
这是最快且最简单的方法:
- (void) countUpH{
sumUp = sumUp + rateInSecH;
//Accessing UI Thread
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//Do any updates to your label here
yourLabel.text = newText;
}];
}
如果您这样做,则无需切换到其他方法。
希望这可以帮助。
Sam
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)