2009-07-25 36 views
0

我在這裏遇到了一些問題,我讓用戶將一些視頻上傳到服務器,但是我在管理用於說明其進度的視圖時遇到一些困難我知道爲什麼這個問題正在發生的事情,我發現它周圍的方式(在某種程度上)在這裏我的問題在iPhone上運行另一個進程時管理視圖

所以,如果一個人試圖做一些代碼,看起來像這樣(在一個UIViewController

-(void)uploadMovie 
{ 
    UIActivityView indicator=new... 
    [self.view addSubview:indicator] 
    [uploader UploadMyMovie:data] 

} 

此代碼不會工作,上傳者將鎖定控制器,並且不會讓指示符及時顯示在屏幕上,我發現等待幾秒鐘重新呼籲上傳者的作品,但我採取了另一種方法。

該方法是爲上傳器啓動一個新線程,並有一個協議,上傳器對象通知UIViewController(或某個委託)何時開始上傳,其進度以及何時完成上傳。喜歡的東西

 -(void)uploadMovie 
    { 
     UIActivityView indicator=new... 
     [self.view addSubview:indicator] 
     NSThread *thread=... 
     [thread start] 
    } 

委託方法是這個樣子

#pragma mark UploadProgressDelegate 

-(void)didStartUploading 
{ 

    progressLabel= [[UILabel alloc] initWithFrame:CGRectMake(93, 240, 116, 32)]; 
    ind= [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    ind.center=self.view.center; 
    [progressLabel setTextColor:[UIColor blackColor]]; 
    [progressLabel setText:@"TEST"]; 
    [self.view addSubview:progressLabel]; 
    [self.view addSubview:ind]; 
    [ind startAnimating]; 
    [self.view bringSubviewToFront:progressLabel]; 
    [ind setHidesWhenStopped:TRUE]; 
    [self.view setUserInteractionEnabled:FALSE]; 
} 
-(void)progressUpdate:(NSString*)progress 
{ 
    [progressLabel setText:progress]; 
} 
-(void)didEndUploading; 
{ 
    [progressLabel removeFromSuperview]; 
    [ind stopAnimating]; 
    [ind removeFromSuperview]; 
    [progressLabel release]; 
    [ind release]; 
    [self.view setUserInteractionEnabled:TRUE]; 
} 

這個偉大的工程和指示器顯示和一切,然後我決定讓用戶看到通過添加一個UILabel進展(反映在上面的代碼),然而對於這個解決方案不起作用,標籤不顯示,當然也沒有更新...

我想知道是否有人遇到這種情況,並已爲它提出瞭解決方案?或者,如果你都不可能從上面爲什麼標籤心不是顯示代碼見...

感謝

回答

1

UIKit不是線程安全的。如果您正在更新UI元素,則需要重新進入主線程或者所有投注均已關閉。

1

在某些情況下,我發現,我需要回到主線程做某些事情。 ..

所以在你的委託方法你會做

[self performSelectorOnMainThread: @selector(updateLabel:) withObject:newLabelText waitUntilDone:NO]; 

然後

- (void) updateLabel:(NSString *)newLabelText 
{ 
    [progressLabel setText:newLabelText]; 
} 

雖然我不確定在主線程而不是在後臺執行什麼操作需要遵循什麼規則。

+0

爲什麼有人標記這個人,他不知道從哪裏j必須打這個電話,但我不得不打電話,謝謝你的幫助 – Daniel 2009-07-25 16:30:56

+0

謝謝你的支持:-) – 2009-07-25 18:15:12

相關問題