2016-12-25 71 views
3

與此類似的大多數問題的問題是在viewDidAppear之前調用present。這不是原因。警告:嘗試在其視圖不在窗口層次結構中的UISplitViewController上呈現UIAlertController

此應用程序不使用NIB的Storyboard,並且所有操作都是編程式的。

該應用程序窗口的rootViewControllerUISplitViewController。分割視圖的視圖控制器被設置爲兩個UINavigationController的數組。子視圖控制器然後模態地呈現視圖控制器。問題是從應用程序委託中呈現的UIAlertController在模態呈現視圖控制器時不會顯示。否則工作。

如何我嘗試提出:

window?.rootViewController?.present(alert, animated: true, completion: nil) 

我得到這個錯誤:

Attempt to present UIAlertController on UISplitViewController whose view is not in the window hierarchy 

回答

2

爲了解決這個問題,我把這個功能在我的應用程序委託。這裏提出

// Utility function to avoid: 
// Warning: Attempt to present * on * whose view is not in the window hierarchy! 
func showAlertGlobally(_ alert: UIAlertController) { 
    let alertWindow = UIWindow(frame: UIScreen.main.bounds) 
    alertWindow.windowLevel = UIWindowLevelAlert 
    alertWindow.rootViewController = UIViewController() 
    alertWindow.makeKeyAndVisible() 
    alertWindow.rootViewController?.present(alert, animated: true, completion: nil) 
} 
+1

相當類似的解決方案是:http://stackoverflow.com/a/34487871/1187415,爲雨燕3瀏覽:http://stackoverflow.com/a/40401936/1187415。 –

相關問題