【问题标题】:Warning: Attempt to present UIAlertController on UISplitViewController whose view is not in the window hierarchy [duplicate]警告:尝试在其视图不在窗口层次结构中的 UISplitViewController 上呈现 UIAlertController [重复]
【发布时间】:2016-12-25 17:06:10
【问题描述】:

与此类似的大多数问题的问题是presentviewDidAppear 之前被调用。这不是这里的原因。

此应用不使用 NIB 的 Storyboard,所有操作都是程序化的。

应用程序窗口的rootViewControllerUISplitViewController。拆分视图的视图控制器设置为两个UINavigationControllers 的数组。然后子视图控制器模态地呈现视图控制器。问题是在以模态方式呈现视图控制器时,从应用程序委托呈现的UIAlertControllers 不会显示。否则它会起作用。

我如何尝试呈现:

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

我收到此错误:

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

【问题讨论】:

    标签: ios swift uikit uisplitviewcontroller uialertcontroller


    【解决方案1】:

    为了解决这个问题,我把这个函数放在我的应用委托中。

    // 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)
    }
    

    【讨论】:

    猜你喜欢
    • 2020-08-07
    • 2019-12-25
    • 1970-01-01
    • 2023-03-31
    • 2018-06-06
    • 2016-11-29
    • 2017-07-27
    • 2014-11-19
    • 1970-01-01
    相关资源
    最近更新 更多