title: Deallocate view controllers in navigation controller that have a reference to self
body: <p>Say I have view controllers A, B, C, D &amp; E all embedded in a navigation controller. In view controller B, I have a custom UIImageView object. In C, I have a custom UITextfield object. Both custom classes have a reference to the view controller for various reasons such as I have to perform things like segue when a user taps the image view. To accomplish this, I have this inside each custom class file:</p>
  
  <pre><code>var controller: UIViewController?
  </code></pre>
  
  <p>And then inside each view controller, inside viewDidLoad I set that variable to self and everything works as expected (segues on tap etc..)</p>
  
  <p>I have an unwind segue from E back to A. However, I noticed that due to these custom objects in view controllers B &amp; C, both were not being deallocated due to a retain cycle caused by having this reference to the view controller. I fixed the issue by setting the controller variable to nil upon segue, however this creates a problem such that if the user goes back (pops the current view controller), because I set the controller variable to nil upon segue, nothing works (it wont segue again because controller var = nil). I thought I might fix this by adding viewWillAppear code as follows:</p>
  
  <pre><code> override func viewWillAppear(_ animated: Bool) {
      usernameTextField.controller = self
      passwordTextField.controller = self
  }
  </code></pre>
  
  <p>Because I read that viewWillAppear will be called each time the viewcontroller comes into view. This did not fix the problem.</p>
  
  <p>Any ideas on how to go about this? How can I set the controllers to nil during the unwind maybe...?</p>