2012-05-19 34 views
9

我需要檢查是否仍然存在根視圖控制器上的模態視圖。 我面臨的問題是我有第二個模式視圖來自需要顯示的某個線程。我想延遲第二個模態視圖,直到第一個模態消失。 因爲第二個模態視圖是有條件的,我不能在第一個被解散後啓動它。如何檢查模態視圖是否正在通過self.window.rootViewController?

[self.window.rootViewController presentModalViewController:vc animated:YES]; 

我想要做的(隨意提出一個更好的替代方法)內容:

  1. 檢查self.window.rootViewController目前在頂部顯示一個模式視圖(或仍然動畫模式視圖)。
  2. 再次使用performSelector:afterDelay:0.1
  3. 檢查,並在必要時再次推遲

回答

13

獲取rootViewController.presentedViewController(在iOS 5.0+以上)或rootViewController.modalViewController(iOS版2.0+以上),看看它是否爲零。

此外,您希望從輔助線程呈現第二視圖控制器,所有的UI東西要在主線程上完成。

+0

這是對的嗎? 「dispatch_async(dispatch_get_main_queue(),^ { MedicationReminderViewController * VC = [[MedicationReminderViewController的alloc] initWithNibName:@」 MedicationReminderViewController 「束:無]; [theDelegate.window.rootViewController presentModalViewController:VC動畫:YES]; });」 – Pieter

+0

@Pieter,這是好的,是的,這段代碼將在主線程上執行 – tux91

+0

我想,rootViewController.modalViewController是任何modalViewController,顯示模態上的rootViewController? – Pieter

0

我會試着做一些更確定一些。有一段代碼負責做兩個模態顯示,並讓它跟蹤第一個模態出現和消失的時間,以便如果它接收到顯示第二個模態的消息,它知道是否執行該操作,或只是設置一個「掛起「國旗。當被告知第一個被解僱時,它可以檢查第二個是否正在等待處理。

1

我也遇到過這樣的問題。我想從推動中彈出一個模式,在此之前我想檢查是否已經顯示了某個屏幕,如果是,請忽略這些屏幕,然後彈出我的屏幕是代碼。

// Dismiss all the modals which are currently shown. 
    - (void) dismissAllModalsIfAnyWithCompletion:(void(^)(BOOL success)) completion{ 

      BOOL hiddenByModal = nil != [[UIApplication sharedApplication].keyWindow.rootViewController presentedViewController]; 
     if (hiddenByModal) { 
//We need to dismiss the existing modal and after completion pop up the new modal. 
      [[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:NO completion:^{ 
       // After dismissing let the handler know. 
       completion(YES); 
      }]; 
     } 
     else{ 
// If there is no modal, then simply let the handler know to pop the new modal. 
      completion(YES); 
     } 
    } 
+1

不錯的優雅的解決方案。 (儘管BOOL標誌看起來有些過於複雜,爲什麼不只是'if([[UIApplication sharedApplication] .keyWindow.rootViewController presentedViewController])?) –

相關問題