2

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.

user2363025
  • 6,365
  • 19
  • 48
  • 89
  • You should not rely on deinit to remove observers. You can use a custom method that is called explicitly to do so. Refer : http://stackoverflow.com/a/18560069/5060335 – UditS Dec 16 '15 at 12:24
  • @Udits I can put in the remove observer in my method where I am setting the active tab, no problem, but I was just worried about the other things that happen in the background when deinit is called. (arc doing its thing, freeing up of memory mainly) and how I could set the active tab when in a child view controller the most efficient way – user2363025 Dec 16 '15 at 12:29

1 Answers1

3

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.

Woodstock
  • 22,184
  • 15
  • 80
  • 118
  • thank you for your response. i'm just trying to make it all crystal clear in my head.. So what I would like to happen is in my method for setting the active tab and removing an observer, I also want to popToRoot so that child controller is no longer active after switching tabs. I'll update the question to reflect the new issue after reading and understanding things a bit better from reading your answer – user2363025 Dec 16 '15 at 12:38
  • @user2363025 - no probs! - yes that is the answer, if you also pop to root VC then the child controller will be destroyed. – Woodstock Dec 16 '15 at 12:40
  • I put the popToRoot in the method where I was switching tabs and moved the tabController.setSlectedIndex into my viewdidDisappear, thanks! – user2363025 Dec 16 '15 at 12:55
  • sorry but I have one additional question. My approach of adding the setSelectedIndex in my viewDidDisappear is working as I want it to. However..I don't understand why when I press the nav back bar button in the child view controller and exit that screen normally, the tab bar does not switch? As I do not have any conditions in viewDidDissappear of whether or not to set the selected index.. Hope I have explained myself clearly. – user2363025 Dec 16 '15 at 14:40
  • I don't fully understand, can you add code and question to the above? – Woodstock Dec 16 '15 at 14:43