0

I'm having trouble figuring out exactly when UIViewControllers are expected to deallocate. I created the simplest of tests, in which I have 3 UIViewControllers on a storyboard, each with a button which performs a segue to the next, so 1->2->3->1->.. and so forth. I then log the appear/disappear load and init/deinit methods, however the deinit is never called.

The log looks like this:

<StoryBoardTest.TestViewController: VC1> init
<StoryBoardTest.TestViewController: VC1> View did load
<StoryBoardTest.TestViewController: VC1> View did appear
<StoryBoardTest.TestViewController: VC2> init
<StoryBoardTest.TestViewController: VC2> View did load
<StoryBoardTest.TestViewController: VC2> View did appear
<StoryBoardTest.TestViewController: VC1> View did disappear
<StoryBoardTest.TestViewController: VC3> init
<StoryBoardTest.TestViewController: VC3> View did load
<StoryBoardTest.TestViewController: VC3> View did appear
<StoryBoardTest.TestViewController: VC2> View did disappear
<StoryBoardTest.TestViewController: VC4> init
<StoryBoardTest.TestViewController: VC4> View did load
<StoryBoardTest.TestViewController: VC4> View did appear
<StoryBoardTest.TestViewController: VC3> View did disappear
<StoryBoardTest.TestViewController: VC5> init
<StoryBoardTest.TestViewController: VC5> View did load
<StoryBoardTest.TestViewController: VC5> View did appear
<StoryBoardTest.TestViewController: VC4> View did disappear
<StoryBoardTest.TestViewController: VC6> init
<StoryBoardTest.TestViewController: VC6> View did load
<StoryBoardTest.TestViewController: VC6> View did appear
<StoryBoardTest.TestViewController: VC5> View did disappear
<StoryBoardTest.TestViewController: VC7> init
<StoryBoardTest.TestViewController: VC7> View did load
<StoryBoardTest.TestViewController: VC7> View did appear
<StoryBoardTest.TestViewController: VC6> View did disappear

This seems like what I would expect if I had these embedded in a NavigationController, but I dont. Can someone shine some light onto why this do not deinit? And as a bonus, how would it differ if this was in a NavigationController.

The VC class:

import UIKit

class TestViewController: UIViewController {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        print("\(self.description) init")
    }

    deinit {
        print("\(self.description) deinit")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("\(self.description) View did load")
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("\(self.description) View did appear")
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        print("\(self.description) View did disappear")

    }

}

The Storyboard looks like so: enter image description here

xceph
  • 1,036
  • 2
  • 13
  • 28
  • Actually your view controllers can't be killed because they are in stack. – Slavik Voloshyn Mar 12 '17 at 11:57
  • So VC never deallocate? There must be a way to remove them as its impossible to transition back to them – xceph Mar 12 '17 at 11:58
  • They deallocate in case if you click for example 'Back'. So, if you click on 'ThirdViewController' back then VC will be cleared from system soon – Slavik Voloshyn Mar 12 '17 at 12:01
  • Okay, that works for going back, but how does one prevent infinite VCs while going forward in this case (Assuming no Navigation controller, and no way to return to the previous VC) – xceph Mar 12 '17 at 12:04
  • Actually you have a strange design in your storyboard. You create every time new instance of ViewController1 from ViewController3. Probably better solution will be use 'unwind' method https://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2 – Slavik Voloshyn Mar 12 '17 at 12:05
  • 'Unwind' method will give you opportunity to move back from ViewContoller3 to ViewController1 without creating new instance – Slavik Voloshyn Mar 12 '17 at 12:08
  • What I am actually looking for, is to move forward in the execution path, 1->2->3->1 while each time creating a new vc, but deallocating the old one once I've moved on. (This is a much simplified example of what I am trying to accomplish, so I realize this example makes no sense) – xceph Mar 12 '17 at 13:21
  • I that case you need at first add UINavigationContoller to your flow and then you will have an access to all viewControllers in stack. As result you can manually remove it from UINavigationContoller's stack. Example http://stackoverflow.com/questions/10281545/removing-viewcontrollers-from-navigation-stack – Slavik Voloshyn Mar 12 '17 at 13:27

0 Answers0