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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11242307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:00:49  来源:igfitidea点击:

How to update a UI label from a non UI thread in iOS

iosxcode

提问by Jitendra kumar jha

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.

我是 iOS 开发的新手,我有简单的目标 -c 类“MoneyTimer.m”来运行计时器,从那里我想用不断变化的计时器值更新 UI 标签。我想知道如何从非 UI 线程访问 UI 元素?我正在使用 Xcode 4.2 和故事板。

In blackberry simply by getting the event lock one can update the UI's from non UI thread.

在黑莓中,只需获得事件锁,就可以从非 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 **
...
}

回答by shoughton123

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.

如果你这样做,你就不必切换到不同的方法。

Hope this helps.

希望这可以帮助。

Sam

山姆

回答by spring

Your question doesn't give much info or detail so it is hard to know exactly what you need to do (e.g. if there is a "thread" issue at all, etc.).

您的问题没有提供太多信息或细节,因此很难确切知道您需要做什么(例如,是否存在“线程”问题等)。

At any rate, assuming your MoneyTimer instance has a reference to the current viewController you can use performSelectorOnMainThread.

无论如何,假设您的 MoneyTimer 实例具有对当前 viewController 的引用,您可以使用performSelectorOnMainThread.

//

//

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;

回答by BBog

I've done something identical in the past.

我过去做过类似的事情。

I used a function to set the label text:

我使用了一个函数来设置标签文本:

- (void)updateLabelText:(NSString *)newText {
    yourLabel.text = newText;
}

and then called this function on the main thread with performSelectorOnMainThread

然后在主线程上使用performSelectorOnMainThread调用这个函数

NSString* myText = @"new value";
[self performSelectorOnMainThread:(@selector)updateLabelText withObject:myText waitUntilDone:NO];

回答by RawMean

The proper way is this:

正确的方法是这样的:

- (void) countUpH  {
   sumUp = sumUp + rateInSecH;
   //Accessing UI Thread
   dispatch_async(dispatch_get_main_queue(), ^{

   //Do any updates to your label here
    yourLabel.text = newText;
   });
}

回答by LJ Wilson

Assuming that label lives in the same class:

假设标签位于同一个类中:

    if(nsTimerUp == nil){
        nsTimerUp = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(countUpH) userInfo:nil repeats: YES];
    [self performSelectorOnMainThread:@selector(updateLabel)
                                           withObject:nil
                                        waitUntilDone:NO];

    }

-(void)updateLabel {
    self.myLabel.text = @"someValue";
}