2016-07-25 100 views
1

我在我的應用程序委託中有一個計時器,用戶選擇在另一個不在標籤欄中的viewController中執行該計時器的選擇器所需的時間間隔;選擇時間間隔後一個模式SEGUE用於加載第一的viewController中的UITabBarController時的時間間隔經過我希望應用程序呈現警報嘗試在其視圖不在窗口層次結構中的UITabBarController上呈現UIAlertController

,但它給了我這樣的:警告:試圖提出UIAlertController上的UITabBarController,其視圖不在窗口層次結構中!

這裏是我'使用的代碼:

let alert = UIAlertController(title: "alert", message: "test", preferredStyle: .Alert) 
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil) 

回答

4

第一次嘗試獲得可見視圖控制器。您可以使用此公延做到這一點:

public extension UIWindow { 
    public var visibleViewController: UIViewController? { 
     return UIWindow.getVisibleViewControllerFrom(self.rootViewController) 
    } 

    public static func getVisibleViewControllerFrom(vc: UIViewController?) -> UIViewController? { 
     if let nc = vc as? UINavigationController { 
      return UIWindow.getVisibleViewControllerFrom(nc.visibleViewController) 
     } else if let tc = vc as? UITabBarController { 
      return UIWindow.getVisibleViewControllerFrom(tc.selectedViewController) 
     } else { 
      if let pvc = vc?.presentedViewController { 
       return UIWindow.getVisibleViewControllerFrom(pvc) 
      } else { 
       return vc 
      } 
     } 
    } 
} 

然後顯示報警控制器在此VC

+0

謝謝你,它一直 – Theilya

+0

沒問題,夥計! – Dmitry

+0

uitabbarcontroller中的uisplitviewcontroller怎麼樣? – SAHM

相關問題