2

When I initialise the ABCViewController instance to push this controller, like I did it here.

private func showABCController() {
      let storyBoard = UIStoryboard (
            name: "PaymentView", bundle: Bundle(for: ABCViewController.self)
        )
      var abcViewController =  storyBoard.instantiateViewController(withIdentifier: "ABCViewController") as! ABCViewController
      abcViewController.delegate = self
}

The denit method of the view controller is called just after the delegate is set to self even before I push the view controller :-

//ABCViewController.swift
    import UIKit
    protocol ABCViewControllerDelegate : class{
        func backButtonPressed()
    }
    class ABCViewController: UIViewController {
        weak var delegate : ABCViewControllerDelegate?
        override func loadView() {
            super.loadView()
            print("loadView")
        }
        override func viewDidLoad() {
            super.viewDidLoad()

            // Do any additional setup after loading the view.
        }

        deinit {
            delegate?.backButtonPressed()
        }
        /*
        // MARK: - Navigation

        // In a storyboard-based application, you will often want to do a little preparation before navigation
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            // Get the new view controller using segue.destination.
            // Pass the selected object to the new view controller.
        }
        */

    }

My concern why deinit is called? Here I have observed the other methods such as loadview and viewdidload is not called. So before the controller is loaded into the memory how can it be de-initialised.

Himan Dhawan
  • 894
  • 5
  • 23
  • I tried running your code and I don't see any issue with it. There might be something else resulting in `deinit` call. – PGDev Oct 16 '19 at 08:49

1 Answers1

0

You need to dig deeper into how ARC works. In your case, you lost reference to your ViewController strictly after showABCController() method done its job. Possible solutions:

  1. Push your VC from showABCController() method
  2. Keep strong reference to abcViewController outside from method and don't forget to release it after exactly pushing it
O.G.O
  • 149
  • 1
  • 9
  • @HimanDhawan tried what exactly? Can you provide a bit more code please with your test cases – O.G.O Oct 17 '19 at 09:10