Search code examples
iosswiftuinavigationcontrollerobserversdeinit

deinit in child view controllers


I have a tab bar controller and one of the tabs consists of a table view. When clicking on a row I segue to a new child controller.

In this new controller, I have a deinit which removes an observer and prints "controller is de-initialised here".

I can see when I press the back button in the navbar that the de-init is called on this action.

Now... I have another method within that child controller which sets the active tab of the tab bar controller. When this action happens, I do not see the print statement indicating the de-init is not being called in that case.

I'm wondering how to solve this? I'm worried that I'm holding onto extra stuff in memory if deinit isn't called.

Update: In my method for setting the active tab, I would also like to popToRoot on the current active child controller. I tried the following:

self.navigationController!.popViewControllerAnimated(true)
self.tabBarController?.selectedIndex = 2

And I get the following error:

popViewControllerAnimated: called on <UINavigationController 0x15609a200> while an existing transition or presentation is occurring; the navigation stack will not be updated.

This makes sense but I'm wondering, is there any way around this?

Update 2

In order to avoid the issue above, I put popToRoot in my observer and added the tabbar.setSelectedIndex to the viewDidDisappear instead.

func checkForSeg(){
      //self.tabBarController?.selectedIndex = 2
      self.navigationController!.popViewControllerAnimated(true)
 }

 override func viewDidDisappear(animated: Bool) {
      super.viewDidDisappear(true)
      print("view Did Disappear called?")
      self.tabBarController?.selectedIndex = 2
 }

But I do not understand why the tab bar isn't switching index when I press the nav back bar button(I don't want it to and it isn't but I dont understand why?)

When I explicitly call popToRoot in the checkForSeg func, then the viewDidDisappear executes the line 'tabbar.setSelectedIndex' (as desired) but when I press the nav back bar button it doesn't (also desired). I don't understand why this is? As I have not implemented any custom logic for when it should or shouldn't perform the tab change.

Also, I AM getting the print statement every time.


Solution

  • When you switch tabs in a UITabBarController, you do not release the previous tab's controller from memory.

    This is expected behavior.

    If you move back to the tab you are referring to you will see that the child VC is still alive and well.

    The current active VC of any tab will always stay resident in memory during the execution of the application.

    Thus, at a minimum, when using UITabBarControllers, the root viewController on each tab is held in memory and only released on application termination, or termination of the tabBarController.

    No issue here.