简体   繁体   English

我的viewcontroller在show segue之后没有被取消初始化

[英]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. 我创建了两个视图控制器,每个控制器都有一个按钮,可以显示另一个视图控制器的segue。

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: 似乎deinit永远不会被调用,每次我返回ViewController时,计数器都在增加:

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. 我认为show segue应该从内存中释放调用者,因为它不再需要。 Am i creating new ViewController objects every time I segue now? 我现在每次创建时都会创建新的ViewController对象吗?

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. 由于您使用的是“show”segue,因此每次都要创建一个新的视图控制器实例,从而有效地呈现顶级视图控制器的“堆栈”。

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 如果要在视图控制器的单个实例之间前后切换,则可以使用容器视图控制器,例如UITabBarController或使用UINavigationController并操作viewControllers属性

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

UIApplication.shared.keyWindow?.rootViewController = VCTwo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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