在执行另一种方法时更新标签文本(动态) [英] Updating label text (dynamically) while executing another method

查看:17
本文介绍了在执行另一种方法时更新标签文本(动态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 iOS 开发人员,目前正在开发一款 iphone 应用程序.我有一个关于更新 ViewController 内容的简单问题,如果我能得到知道此问题或有建议解决方案的人的反馈,我将不胜感激.

I am an iOS developer currently developing an iphone application. I have a simple question regarding updating the contents of a ViewController and I would greatly appreciate it if I can get someone’s feedback who is aware of this issue or have a suggested solution.

我正在编写一个不断更新标签文本的方法(以及其他组件,例如 UIImage box1" 和 box2" 的背景颜色).

I’m writing a method that constantly updates the text of a label (and other components such as the background colour of a UIImage "box1" and "box2" ).

问题:对标签文本的更新(以及其他更改)仅在代码执行完毕后才生效.所以我写了一个简单的无限循环,如下所示:

The issue : update to the text of the label (along with other changes) only takes effect at the end when the code was done execution. So I have written a simple infinite loop that does this as follows:

-(void) stateTest{
    int i=0;
    for(;;){
       if (i==5) {
          self.stateLabel.text=@"Opened"; //stateLabel previously declared
          [self.box2 setBackgroundColor:[UIColor whiteColor]]; // box2 declared as UIImage
          [self.box1 setBackgroundColor:[UIColor purpleColor]];
        }
        if (i==10){
            self.stateLabel.text=@"Closed";
            [self.box2 setBackgroundColor:[UIColor redColor]];
            [self.box1 setBackgroundColor:[UIColor whiteColor]];
            break;
        }
        i++;
    }

}

在 viewDidLoad 方法中:

and in the viewDidLoad method:

- (void)viewDidLoad
{       
    [super viewDidLoad];
    self.stateLabel.text=@"View did Load";
    [self.box1 setBackgroundColor:[UIColorwhiteColor]];
    [self.box2 setBackgroundColor:[UIColorwhiteColor]];   
    [self stateTest];

}

我正在逐步执行代码(断点),这就是我面临的问题:

I’m stepping through the code (breakpoints) and this is the issue I’m facing:

  1. 当 i==5 时,标签文本不会更新(与框的颜色相同)
  2. 当 i==10 时,标签文本不会更新
  3. 在代码执行完成后显示更新(标签文本和框颜色)stateLabel : "已关闭",方框 2:红色背景色

我尝试在 stateTest 方法的末尾(在 i++ 之后)调用几个函数,希望它能刷新"视图、标签和/或 UIImage 的内容:

I have tried calling upon several functions at the end of the stateTest method (after the i++) hoping that it will "REFRESH" the contents of the view, label and/or the UIImage:

    [self.stateLabel setNeedsLayout];
    [self.stateLabel setNeedsDisplay];
    [self.box1 setNeedsDisplay];
    [self.box2 setNeedsDisplay];
    [self.view setNeedsDisplay];

不幸的是,这些试验都没有奏效.我还放置了一个输出标签文本的 NSLog,它可以正常工作,如我所愿.但问题在于代码/方法执行过程中视图控制器内容的动态更新.

Unfortunately non of these trials worked. I have also put an NSLog that outputs the text of the label and that works just fine, as I want. But the problem is with the dynamic update of the contents of the view controller during execution of code/method.

我将不胜感激任何帮助,我愿意接受任何建议

I would greatly appreciate any help and I am open to any suggestion

理想情况下,此代码与检测嘴巴状态的人脸检测算法并行使用.有一个 processImage 方法可以处理每一帧的图像.每次处理图像时我都会调用[self stateTest];如果嘴是张开的 → stateLabel.text=@"Open"…etc

Ideally, this code is used in parallel with a face detection algorithm that detects the state of the mouth. There is a processImage method that processes the image every frame. I call [self stateTest] every time the image is processed; if the mouth is opened → stateLabel.text=@"Open"…etc

提前感谢您的贡献干杯!

Thank you in advance for your contribution Cheers!

推荐答案

UI 更新只会在 run loop 完成时发生.通常我在后台线程中运行无限循环并在主线程中发布 UI 更新.这将处理运行循环.

The UI update will happen only when the run loop is completed. normally i run the infinite loop in background thread and post the UI update in the main thread. This will take care of the run loop.

以下代码供参考.

    -(void) viewDidLoad
    {
        [super viewDidLoad];
        self.subView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 30, 30)];
        self.subView.backgroundColor = [UIColor blueColor];

        [self.view addSubview:self.subView];



        dispatch_queue_t newQueue = dispatch_queue_create("newQueue", nil);
        dispatch_async(newQueue, ^{
            [self stateTest];
        });

    }

    -(void) stateTest{
        int i=0;
        for(;;){
            if (i==5) {
                [self performSelectorOnMainThread:@selector(purpleColor) withObject:self waitUntilDone:NO];

            }
            if (i==10){
                [self performSelectorOnMainThread:@selector(blueColor) withObject:self waitUntilDone:NO];
              //  break;
            }
            if (i==15){
                [self performSelectorOnMainThread:@selector(redColor) withObject:self waitUntilDone:NO];
                //break;
            }
            if (i==20){
                [self performSelectorOnMainThread:@selector(greenColor) withObject:self waitUntilDone:NO];
                break;
            }
            sleep(1);
            i++;
        }
    }

    -(void) purpleColor
    {
        [self.subView setBackgroundColor:[UIColor purpleColor]];
    }

    -(void) blueColor
    {
        [self.subView setBackgroundColor:[UIColor blueColor]];
    }

    -(void) redColor
    {
         [self.subView setBackgroundColor:[UIColor redColor]];
    }

    -(void) greenColor
    {
        [self.subView setBackgroundColor:[UIColor greenColor]];
    }

这篇关于在执行另一种方法时更新标签文本(动态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆