[英]Child view controller should have parent view controller but actual parent is (UIViewControllerHierarchyInconsistency)
我有一个将 UITabbarController 作为初始窗口的应用程序(登录后我将其设置为 rootViewController)
这个 tabbarController 有 4 个不同的选项卡,它们都是嵌入在 UINavigationController 中的 UIViewController。
在我的 tabbarController 中,当我进行初始配置时,我设置了如下标签:
首先我创建viewControllers:
let tableViewController1 = tableViewController1(nibName: "tableViewController1", bundle: nil) let tableViewController2 = tableViewController2(nibName: "tableViewController2", bundle: nil) let tableViewController3 = tableViewController3(nibName: "tableViewController3", bundle: nil) let profileViewController = ProfileViewController(nibName: "ProfileViewController", bundle: nil)
然后我将它们嵌入到导航控制器中:
let navController1 = UINavigationController(rootViewController: tableViewController1) let navController2 = UINavigationController(rootViewController: tableViewController2) let navController3 = UINavigationController(rootViewController: tableViewController3) let navController4 = UINavigationController(rootViewController: profileViewController)
最后,将 navigationControllers 添加为 tabbarController 的 viewControllers
viewControllers = [navController1,navController2,navController3,navController4]
我在网上查了一下,这种使用方式似乎是正确的。 例如: https ://medium.com/@ITZDERR/uinavigationcontroller-and-uitabbarcontroller-programmatically-swift-3-d85a885a5fd0。 但是我从我的实时应用程序收到崩溃日志,如下所示
Crash, child view controller:<UINavigationController: 0x160b3400>
should have parent view controller:<ProfileViewController: 0x16098800>
but actual parent is:<MyApp.MainTabBarController: 0x16064c00>
这里的问题是崩溃说profileViewController应该是navigationController的父级但是我在navigationController中嵌入了profileViewController并添加为tabbarController的viewControllers,所以在我看来这个顺序是正确的。
我搜索了此崩溃,似乎我正在跳过使用 addChildViewController 或在某处删除父级的步骤,但无法弄清楚。
我找到了与此相关的链接,但没有一个帮助我解决了这个问题。
示例链接:
遇到同样的问题,添加layoutifneeded()
对我有用。 下面的例子:
navigationController.setViewControllers([], animated: false)
navigationController.view.layoutIfNeeded()
问题是navigaionController
不会立即删除viewControllers
。 layoutifneeded
告诉系统立即重绘。 所以不会发生崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.