加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何从iOS中的非UI线程更新UI标签

发布时间:2020-12-15 02:03:02 所属栏目:百科 来源:网络整理
导读:我是iOS开发的新手,我有简单的目标-c类“MoneyTimer.m”用于运行定时器,从那里我想更新一个UI标签与定时器的变化值。 我想知道如何从非UI线程访问UI元素?我正在使用Xcode 4.2和storyboarding。 在黑莓中,通过获取事件锁可以从非UI线程更新UI。 //this th
我是iOS开发的新手,我有简单的目标-c类“MoneyTimer.m”用于运行定时器,从那里我想更新一个UI标签与定时器的变化值。
我想知道如何从非UI线程访问UI元素?我正在使用Xcode 4.2和storyboarding。

在黑莓中,通过获取事件锁可以从非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;

   }];
}

如果你这样做,你不需要切换到另一种方法。

希望这可以帮助。

山姆

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读