AppDelegate之外,我想更改它的rootViewController。换句话说,我想在窗口中放置一个新的导航控制器,然后将其推入该导航控制器中:

func JumpToPage(_ controller: UIViewController) {
        guard let rootController = AppDelegate.shared?.presentationViewController else {
            return
        }
        let navigationController = UINavigationController(rootViewController: rootController)
        navigationController.pushViewController(controller, animated: true)
        AppDelegate.shared?.window?.rootViewController = navigationController
    }
}


但是此功能导致应用程序崩溃并显示以下错误消息:

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency',
    reason: 'adding a root view controller <MyExampleController> as a child of view controller:<UINavigationController>'


我应该如何解决?

最佳答案



let navigationController = UINavigationController(rootViewController: rootController)




let navigationController = UINavigationController(rootViewController: controller)


更新资料

func JumpToPage(_ controller: UIViewController) {
    guard let rootController = AppDelegate.shared?.presentationViewController else {
        return
    }
    let navigationController = UINavigationController(rootViewController: controller)
    AppDelegate.shared?.window?.rootViewController = navigationController
}

关于ios - 更改AppDelegate的根 View Controller 会导致UIViewControllerHierarchyInconsistency异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52587470/

10-14 21:33