简体   繁体   中英

Child view controller should have parent view controller but actual parent is (UIViewControllerHierarchyInconsistency)

I have an app which has UITabbarController as the initial window(after logging-in i set it as rootViewController)

This tabbarController has 4 different tabs in it which are all UIViewControllers embedded in a UINavigationController.

In my tabbarController when i am making the initial configurations i set tabs like below:

  • First I create the 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)
  • Then I embed them inside navigationControllers:

     let navController1 = UINavigationController(rootViewController: tableViewController1) let navController2 = UINavigationController(rootViewController: tableViewController2) let navController3 = UINavigationController(rootViewController: tableViewController3) let navController4 = UINavigationController(rootViewController: profileViewController)
  • Finally, add the navigationControllers as tabbarController's viewControllers

    viewControllers = [navController1,navController2,navController3,navController4]

I looked it up online and this way of using it seems correct. Ex: https://medium.com/@ITZDERR/uinavigationcontroller-and-uitabbarcontroller-programmatically-swift-3-d85a885a5fd0 . However i'm receiving crash logs from my live app as below

Crash, child view controller:<UINavigationController: 0x160b3400>
should have parent view controller:<ProfileViewController: 0x16098800>
but actual parent is:<MyApp.MainTabBarController: 0x16064c00>

The problem in here is that the crash says profileViewController should be the parent of the navigationController however i embedded profileViewController inside the navigationController and added as tabbarController's viewControllers, so in my opinion the order is correct.

I searched for this crash and it seems like i'm skipping a step of either using addChildViewController or removing a parent somewhere, but couldn't figured out.

I have find related link to this but non-of them helped me to solve this issue.

Example links:

Ran into the same issue, and adding layoutifneeded() worked for me. example below:

navigationController.setViewControllers([], animated: false)
navigationController.view.layoutIfNeeded()

The problem is the navigaionController doesn't remove viewControllers immediately. layoutifneeded tells the system to redraw immediately. So the crash won't happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM