我是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 **
...
}
Run Code Online (Sandbox Code Playgroud)
sho*_*123 34
这是最快捷,最简单的方法:
- (void) countUpH{
sumUp = sumUp + rateInSecH;
//Accessing UI Thread
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
//Do any updates to your label here
yourLabel.text = newText;
}];
}
Run Code Online (Sandbox Code Playgroud)
如果以这种方式执行此操作,则无需切换到其他方法.
希望这可以帮助.
山姆
您的问题没有提供太多信息或细节,因此很难确切地知道您需要做什么(例如,是否存在“线程”问题等)。
无论如何,假设您的 MoneyTimer 实例具有对当前 viewController 的引用,您可以使用performSelectorOnMainThread。
//
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25803 次 |
| 最近记录: |