【问题标题】:Display output in infinite loop iOS在无限循环iOS中显示输出
【发布时间】:2013-03-31 22:48:51
【问题描述】:

我是 iOS 新手,我需要一些关于如何在 Iphone 屏幕上无限循环显示输出的反馈。

按下按钮时,应用程序进入无限循环并创建一些输出。我想在 Iphone 屏幕上显示这些输出。尽管我在调试屏幕上从 printf 获得了这些值,但 Iphone 屏幕上没有显示任何内容。如果我在没有无限循环的情况下尝试这个功能,我将能够在屏幕上看到输出,但是应用程序必须在无限循环中运行。

由于我是编程新手,应用程序是否需要在多线程模式下运行?

代码如下:

-(IBAction)button:(UIButton *)sender{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        int output = -1;

        while (pool) {

            //output is received

            if((output != -1) ) {

                printf("%i \n", output); // the output is seen on debug screen

                self.display.text = [NSString stringWithFormat:@"%i",output]; // the outputs is not shown on iphone

                //[pool release];

            }
        }
    }

【问题讨论】:

标签: ios loops text label infinite


【解决方案1】:

你没有改变输出的值,所以如果它进入循环它永远不会改变。

该应用程序已经是多线程的,否则不可能 - 您可能正在谈论后台任务(http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

您是否应该在不同的线程上取决于循环的控制方式,任何繁重的工作或大量的大量工作都应该从主线程中取出,类似于:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //My background stuff

    dispatch_async(dispatch_get_main_queue(), ^{
        //My ui code
    });
});

关于线程,GCD 使用起来非常简单:(https://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html) - 有很多关于 GCD 的教程

【讨论】:

    【解决方案2】:

    我认为问题是快速变化的文本不让我们看到输出。

    试试这个,告诉我你得到了什么。

    self.display.text = [NSString stringWithFormat:@"%@%i",self.display.text, output];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 2014-01-04
      相关资源
      最近更新 更多