0

我想在那一刻我UIAlert我上面的UIActivityIndi​​catorView看,我有這樣的代碼:認沽UIAlertView中上述UIActivityIndi​​catorView

- (void)showWithLabelDeterminate{ 

    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; 
    [self.navigationController.view addSubview:HUD]; 

    // Set determinate mode 
    HUD.mode = MBProgressHUDModeDeterminate; 

    HUD.delegate = self; 
    HUD.labelText = @"Loading"; 

    // myProgressTask uses the HUD instance to update progress 
    [HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES]; 
} 

- (void)myProgressTask 
{ 
    float progress = 0.0f; 

    while (progress < 0.99f) 
    { 
     progress += 0.01f; 
     HUD.progress = progress; 
     usleep(50000); 

     if(progress > 0.02f & progress < 0.05f) 
     { 
      NSString * string = [NSString stringWithFormat:@"http://localhost:8080/......"]; 
      NSURL *url = [NSURL URLWithString:string]; 

      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:60.0]; 

      urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

      if(!urlData) 
      { 
       [self simpleAlert]; 

      } 
      else 
      { 
        datos = [[NSMutableString alloc]initWithData:urlData encoding:NSISOLatin1StringEncoding]; 
      } 
     } 
    } 
} 

-(void)simpleAlert 
{ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Dont connect!!" delegate:self cancelButtonTitle:@"Refresh" otherButtonTitles:nil]; 
    [alertView show]; 
    [alertView release]; 
} 

的問題是,我沒有看到我上面的UIActivityIndi​​catorView我的UIAlertView中.....

回答

1

myProgressTask要麼在後臺線程上執行,要麼阻止主線程調用usleep

如果它在後臺線程上,那麼您對UIAlertView的使用是無效的,因爲在一般情況下,UIKit只能在主線程中使用。最快的解決方案是使用performSelectorOnMainThread:...撥打simpleAlert

如果它在主線程上,那麼你可能會阻止UIKit的運行。關於使用非阻塞NSURLConnection並使用NSTimerperformSelector:withObject:afterDelay:可以在暫停後安排事情發生的明顯評論。

+0

我想完成我的UIActivityIndi​​catorView,因爲在這一刻它不會是必要的。 – Davidin073

+0

在發佈的代碼中沒有UIActivityIndi​​catorView,推測它是內置於MBProgressHUD的?如果我不得不猜測,我會這樣說,在我的兩個猜測中,可能你想改變你對simpleAlert的調用,以便在主線程上發佈它。 – Tommy

+0

是的,是的,對不起湯米,我提到MBProgressHUD,是的,我想關閉這個progressHUD並初始化我的UIAlertView。 – Davidin073

相關問題