2012-02-27 41 views
4

我是一位全新的Mac程序員,我需要關於如何使用NSProgressIndicator的幫助。我已經在尋找示例代碼,但找不到任何有用的東西。如何使用可可進度條?

我想這樣做是:

-(IBAction)startProgressBar:(id)sender; { 

    //I want to make the bar update itself by the value of 1 until it is at the value of 100 
    //Example: add 1 to bar every second until it is full 

} 

回答

8

我認爲performSelector:withObject:afterDelay將幫助你在這裏。

編寫一個可以增加進度條的方法。在該方法結束時,用相同的方法調用performSelector:withObject:afterDelay,延遲1秒,直到條滿。

您可能不需要將對象傳遞給該方法,因此您可以使用nil。

編輯

在你的情況我建議是這樣的:

- (IBAction)startProgressBar:(id)sender 
{ 
    // Initialize the progress bar to go from 0 to 100 
    [progress setMinValue:0.0]; 
    [progress setMaxValue:100.0]; 
    [progress setDoubleValue:0.0]; 

    // Start the auto-increment calls 
    [self incrementProgressBar]; 
} 

- (void)incrementProgressBar 
{ 
    // Increment the progress bar value by 1 
    [progress incrementBy:1.0]; 

    // If the progress bar hasn't reached 100 yet, then wait a second and call again 
    if([progress doubleValue] < 100.0) 
     [self performSelector:@selector(incrementProgressBar) withObject:nil afterDelay:1]; 
} 
+0

我已經知道該行的代碼,但我不知道它,請如何循環給我更多的細節: ) – CocoaCoder 2012-02-27 18:32:53

+1

想象一下,您在最後編寫了一行代碼。該方法運行後,它將再次運行(延遲後)。然後再次。它有點像遞歸方法。 – Aaron 2012-02-27 20:18:23

+0

是的,但我想讓它循環,直到條100%滿,那麼我想停止循環,請幫助:D – CocoaCoder 2012-03-01 19:22:49