簡體   English   中英

UIAlertView加載指示器在輔助線程中不起作用

[英]UIAlertView Loading indicator doesn't work in a secondary thread

我有以下代碼用於UIAlertView Loading指示器,該指示器不起作用並給我

- (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中崩潰。 任何建議...

您正在顯示來自分離線程的警報,而必須從主線程執行警報,請使用GCD或performSelectorOnMainThread


在主線程上,您通常只想執行UI更新,所有復雜的計算和數據加載都將在分離的線程中執行。 如果您嘗試將數據加載到主線程中,則UI在加載期間將無響應。 因此,這是將數據加載到分離線程中的一種好習慣,在主線程上,在加載開始時根據需要顯示警報,並在加載(和解析)完成后將其關閉,並調用內容UI更新:

加載/刷新數據源流

這是一個壞習慣。

蘋果文檔說,您需要處理主線程上的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對我來說工作完美。

嘗試這個

- (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元素。 這就是問題所在。 希望這可以幫助。 編碼愉快。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM