gpt4 book ai didi

ios - 更改AppDelegate的 Root View Controller 导致UIViewControllerHierarchyInconsistency异常

转载 作者:行者123 更新时间:2023-11-30 11:07:07 26 4
gpt4 key购买 nike

AppDelegate之外,我想更改它的rootViewController。换句话说,我想在窗口中之前放置一个新的导航 Controller ,然后将新的 Controller 推送到此导航 Controller 中:

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的 Root View Controller 导致UIViewControllerHierarchyInconsistency异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52587470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com