2012-11-05 38 views
-1

我有以下的這些不正常的UIAlertView中加載指示燈代碼,並給了我UIAlertView中加載指示燈沒有在輔助線程工作

- (void) launchActivity 
{ 
//some logic... 
[NSThread detachNewThreadSelector:@selector(updateFilterProgress) toTarget:self withObject:nil]; 
} 
- (void) updateFilterProgress { 
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) 
{ 
    UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"No Internet Connectivity" message:@"This app require an internet connection via WiFi or cellular network to work." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease]; 
    [myAlert show]; 
} 
else{ 
    UIAlertView *alertMe = [[[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil] autorelease] ; 


    //tried this way by placing below line....no result 
    [alertMe performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES]; 
    //[alertMe show]; 

//some logic... 
} 

更新時間: 在我打電話的主線程Web的服務和加載數據。因此我給了另一個加載UIAlertView的線程,它與iOS 4,5一起工作。但它在iOS 6中崩潰。如果我將AlerView放在主線程上,那麼在加載任何內容時都不顯示,但在獲取數據後AlertView會顯示加載指示器幾秒鐘。任何建議...

回答

1

您顯示的是分離線程的警報,而您必須在主線程中執行此操作,請使用GCD或performSelectorOnMainThread


在主線程上,您通常只想執行UI更新,所有複雜的計算和數據加載都將在分離的線程中執行。如果您嘗試在主線程中加載數據,則在加載期間UI將不會響應。因此,這是一種很好的做法,可以在加載啓動時顯示警報的主線程中加載分離線程中的數據,並在加載(和解析)完成時關閉它,同時調用內容UI更新:

Load/Refresh datasource flow

+0

我編輯了我的問題。您可以檢查並建議.. –

+0

@Navnath Memane當您在主線程上加載數據時,它將被阻止,並且所有UI更改都將在數據加載後排隊等待執行。您需要實現一個反向概念:在分離的線程中加載數據,並在主線程中顯示警報,這樣加載時UI不會被阻塞。但請注意,因爲連接委託消息用於在調用該操作的同一線程上觸發。這意味着如果你做的一切正確,委託調用將在分離的線程中,這就是爲什麼你需要使用GCD在主線程上顯示警報。 –

+0

你的反向技巧就像這裏的魔術一樣。我現在正在調用輔助線程上的Web服務。你可以編輯你的答案,以便我可以接受它。 –

1

這是一個不好的做法。

Apple文檔說,您需要處理主線程上的UI元素。

我認爲這個問題是這一行:

[NSThread detachNewThreadSelector:@selector(updateFilterProgress) toTarget:self withObject:nil]; 

您將無法處理其他線程,而不是在主線程的UI元素。

用途:

[self updateFilterProgress]; 

或者使用像:

[yourAlert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES]; 

此外,我與你的代碼進行覈對。有一個錯誤彈出:

錯誤是:爲 'UIAlertView中' 不可見@interface聲明選擇 'performSelectorOnCurrentThread:withObject:waitUntilDone:'

performSelectorOnMainThread工作完美的我。

+0

如果'launchActivity'是從另一個分離線程調用的,那麼這將不起作用。 –

+1

@ A-Live:謝謝你提及,我編輯了我的答案。謝謝老兄:) –

+0

在主線程上,我打電話給web服務和加載數據。因此我給了另一個加載UIAlertView的線程,它與iOS 4,5一起工作。但它在iOS 6 –

0

試試這個

- (void) launchActivity 
{ 
//some logic... 
[NSThread detachNewThreadSelector:@selector(updateFilterProgress) toTarget:self withObject:nil]; 
} 
- (void) updateFilterProgress { 
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) 
{ 
    [self performSelectorOnMainThread: @selector(showAlertForNoNetConnect)]; 
} 
else{ 
    [self performSelectorOnMainThread: @selector(showAlertForLoading)]; 
//some logic... 
} 

- (void) showAlertForNoNetConnect 
{ 
    UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"No Internet Connectivity" message:@"This app require an internet connection via WiFi or cellular network to work." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease]; 
    [myAlert show]; 

} 
- (void) showAlertForLoading 
{ 
    UIAlertView *alertMe = [[[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil] autorelease] ; 
    [alertMe show]; 

} 

你必須調用在主線程中的所有的UIKit元素。這就是問題所在。 希望這有助於。快樂編碼。 :)

+0

我編輯了我的問題。你可以檢查並建議.. –