Micro
Micro

Reputation: 10891

navigationController?.popViewController(animated: true) doesn't deinit the view controller

When I use navigationController?.popViewController(animated: true) or when I dismiss a UINavigationController, the deinit method of my ViewControllers does not get called. Does this mean they are not being deallocated in memory?

How can I deallocate a UINavigationController and all of its ViewControllers?

Upvotes: 2

Views: 1604

Answers (1)

Jacob Dyer
Jacob Dyer

Reputation: 106

As mentioned above, you may have a variable somewhere that's holding a reference to the view controller. If that's the case, that variable is causing Swift to hold onto your View Controller in memory. You can do a few things to fix that:

  • Set that variable to a weak var, which means that if its the last variable left holding a reference to something, it will be deallocated.
  • Set the variable to nil when you pop the view controller. If you have no references left to the view controller, then it will be deallocated.

Sweepers comment will hopefully help you find the variable in question.

Upvotes: 2

Related Questions