2013-04-09 57 views
-1

我是iOS新手,我需要一些關於如何在無限循環中在Iphone屏幕上顯示輸出的反饋。在無限循環中顯示輸出iOS

當按鈕被按下時,應用程序進入無限循環並創建一些輸出。我想在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]; 

      } 
     } 
    } 
+0

是的,您需要在另一個線程上運行循環,並將結果發佈到UI線程以顯示在屏幕上。有關可能有用的文檔的線程編程指南(https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html) – Marcelo 2013-04-09 13:42:06

回答

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

0

我認爲問題是快速變化的文本不讓我們看到輸出。

試試這個,並告訴我你知道你在得到什麼。

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