2012-03-08 62 views
1

我有這樣的代碼在點擊一個按鈕[NSThread detachNewThreadSelector: @selector(spinBegininapp) toTarget:self withObject:nil];以顯示該後臺線程運行用戶活動的指標,我把這個代碼,以使活動的指示燈如何實現UIProgressView在iPhone SDK的

- (void)spinBegininapp 
{ 
    _activityindictor.hidden = NO; 

} 

它的工作正常,當我點擊按鈕它顯示活動指標動畫,當線程去隱藏活動指標,但我的需要是顯示progressView而不是activity-indicator,它根據線程前進,如果線程完成,它需要完全達到進展並且自己隱藏起來,這是可能的。

回答

3

你可以添加一個進度視圖毫無疑問,但通常它用於確定的數量,如時間或數據..例如。如果你正在下載一個2MB的文件,那麼你總是可以告訴你已經下載了多少數據,並將其作爲一個因素顯示在進度視圖中。所以,如果類似的東西是你的線程裏面發生的事情,你可以使用這個..

UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:whateverStyle]; 
    progressView.progress = 0.75f; 
    [self.view addSubview: progressView] 
    [progressView release]; 

你只需要更新的價值變化的進度....希望這有助於。

+0

是烏爾正確,我在這裏爲更新我的進度,因爲價值changes.i把這個代碼,但沒有運氣filepercentage =(float)1 /(float)10; \t progressview.progress = filepercentage; \t \t int p = 100 * filepercentage; NSString * percent = [NSString stringWithFormat:@「%d %%」,p]; – stackiphone 2012-03-08 08:32:05

+0

「filepercentage =(float)1 /(float)10」你在這裏做什麼?以及你面對的是什麼錯誤......? – 2012-03-08 08:43:08

+0

我沒有在我的progressview中獲得progrees更新。 – stackiphone 2012-03-08 08:47:48

6
#pragma mark - Loading Progress 

static float progress = 0.0f; 

-(IBAction)showWithProgress:(id)sender { 
    progress = 0.0f; 
    _progressView.progress = progress; 
    [self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.3]; 
} 

-(void)increaseProgress { 
    progress+=0.1f; 
    _progressView.progress = progress;  
    if(progress < 1.0f) 
    [self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.3]; 
    else 
    [self performSelector:@selector(dismiss) withObject:nil afterDelay:0.2f]; 
} 

-(void)dismiss { 
    [self progressCompleted]; 
} 

現在調用下面的函數,每當/在任何你想顯示進度

[self showWithProgress:nil]; 

進步的範圍在0.0和1.0之間

1.0是指100%

+0

如何自動更新IBAction的進度,因爲我從服務器下載並且想顯示其進度 – 2014-02-27 10:52:49

+0

這裏是我的[code](http://pastebin.com/QU7r1k7D),進度條只更新一次,即顯示10%並停止 – 2014-02-27 11:16:57