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

1

8

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:

Wyndham answered 16/3, 2020 at 15:1 Comment(3)
Did you find a solution to this? Having similar problemsBoddie
I did solve it, however it's been so long that i do not remember how :_) At least i can make a suggestion which may fix the issue. I have added a 0.1 sec time delay which if i remember correctly fixed it. Hope it becomes useful :)Wyndham
Ah the delay thingy - thanks!Boddie
T
2

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.

Thoughtful answered 15/6, 2022 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.