简体   繁体   中英

My viewcontroller is not being deinitialized after show segue

I have created two view controllers, each with a button that does a show segue to the other view controller.

var counter = 0
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        counter += 1

        print("init viewc \(counter)")
    }

    deinit {
        counter -= 1
        print("deinit viewc \(counter)")
    }
}

It seems like deinit is never called, each time i return to ViewController, the counter is increasing:

init viewc 1
init viewc 2
init viewc 3

And so on... Am i missing something here? I thought show segue was supposed to release the caller from memory since it is no longer needed. Am i creating new ViewController objects every time I segue now?

Since you are using a "show" segue, you are creating a new view controller instance each time, effectively presenting the top an ever deeper "stack" of view controllers.

If you want to switch back and forward between single instances of your view controllers then you could use a container view controller, such as a UITabBarController or use a UINavigationController and manipulate the viewControllers property

如果你试图放弃一个想要保留的view controller ,并且你不打算做一个unwind segue并返回到一个先前的view controller的最新状态,并且你想要释放一个先前的视图控制器,然后您基本上将后一个视图控制器之一视为新的rootViewController ,直到您在AppDelegate上重新分配根视图控制器属性,由于rootViewController具有强引用,因此第一个视图控制器将不会被释放。它。

UIApplication.shared.keyWindow?.rootViewController = VCTwo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM