0

I have two test controllers: TestController1 and TestController2. I'm trying to deinit first controller when second controller loaded or appeared by changing UIApplication.shared.window[0].rootViewController to self (TestController2). But nothing is going on and I also have two controllers in memory. So, my question is: how can I deinit all ViewControllers and keeps in memory only one?

All my test controllers have deinit method and I'm trying to print "deinited" when it's executed, so i don't have any problems with break points like in many questions asked here.

This is my TestController2's code:

class TestController2: UIViewController {

    deinit {
        print("testController 2 deinited")
    }


    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        //first attempt
        let del = UIApplication.shared.delegate as! AppDelegate
        del.window?.rootViewController = self

        //second attempt
        UIApplication.shared.windows[0].rootViewController = self

        //third attempt
        UIApplication.shared.keyWindow?.rootViewController = self
    }
}

All my attempts don't work..

Updated: First and second controllers have only one button. No any business logic. I created a segue by ctrl+drag mode on Storyboard from button at controller 1 to controller 2.

pacification
  • 5,838
  • 4
  • 29
  • 51
Denis Petrov
  • 47
  • 1
  • 8

0 Answers0