2011-08-16 26 views
1

我有一個視圖,其中包含一個按鈕。當用戶按下按鈕,下面的代碼被執行:iOS - UIAlertView在預期之前被解僱了

- (IBAction) goToPhotoViewControllerView:(id) sender{ 

    alert = [[UIAlertView alloc] initWithTitle:@"Por favor, aguarde" message:@"Carregando imagens" 
            delegate:nil 
         cancelButtonTitle:nil 
         otherButtonTitles:nil]; 
    [alert show]; 

    if(alert != nil) { 
     UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 

     indicator.center = CGPointMake(alert.bounds.size.width/2, alert.bounds.size.height-45); 
     [indicator startAnimating]; 
     [alert addSubview:indicator]; 
     [indicator release]; 
    } 

    dispatch_async(dispatch_get_global_queue(0, 0), ^{ 

    //Do a lot of stuff here 

     PhotoViewController *photoViewControllerView = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil]; 

     [iosDao selectComics]; 

     [photoViewControllerView startWithComic:[[iosDao.comics lastObject] idComic] numPanels:[[[iosDao.comics lastObject] panels] count]]; 
     photoViewControllerView.navigationItem.title = @"Comics"; 

     [iosDao release]; 
     [[self navigationController] pushViewController:photoViewControllerView animated:YES]; 

     [alert dismissWithClickedButtonIndex:0 animated:YES]; 
     [alert release]; 
    }); 
} 

什麼情況是,大部分的時間,整個異步代碼運行,駁回警報消息。但是,在警報消息解除後,屏幕在推到下一個viewController(photoViewControllerView)之前仍然保持凍結狀態約2或3秒。我不知道爲什麼會這樣。我希望警報消息只要需要就保持。有任何想法嗎?

預先感謝您!

回答

1

對我來說,這聽起來像問題是PhotoViewController是花費時間加載(你可以使用儀器來確認?)。由於PhotoViewController是您自己的類,您可以讓它有一個委託,使主視圖控制器(解除UIAlert的)控制PhotoViewController實例的委託,並讓PhotoViewController調用委託以在其準備就緒時通知它和/或可見,例如在

- (void)viewWillAppear:(BOOL)animated 
+0

胡姆,這是有道理的。但讓我問你。這是否意味着PhotoViewController被異步分配?我的意思是,不應該在PhotoViewController完成加載後(最後一個在代碼中間,在警告解除之前的一段時間內)完成最後兩行(具有警告解除和釋放的行)? –

+0

我嘗試了你所說的,但它沒有奏效。我把解僱線作爲PhotoViewController的viewWillAppear方法的最後一行。然而,讓我瘋狂的是,它到達了這條線,解除了警報(如預期的那樣),然後,在實際移動到PhotoViewController視圖之前,它凍結屏幕大約3或4秒。我認爲,只要viewWillAppear的最後一行到達,新的視圖將不得不在此後顯示。但它並沒有。移動前,前一個視圖凍結約2或3秒。有誰知道爲什麼? –

1

末你應該只提出了一個警報視圖,並指定自己爲它的委託,然後當你得知警報視圖被駁回(通過alertView:didDismissWithButtonIndex:委託方法),執行其餘你的代碼。

+0

嗨fichek。感謝回覆。我想要做的是在alertView被解除之前執行所有的異步部分。我希望alertView保持運行所需的時間,然後切換viewControllers。 –

+0

哦,只是注意到你的警報視圖沒有按鈕,你只希望它以編程方式關閉。這是告訴人們等待的好方法。 UIActivityIndi​​catorView和一個標籤可以做得很好。 –