我很难弄清楚UIViewControllers何时应该解除分配。我创建了最简单的测试,其中我在故事板上有3个UIViewControllers,每个都有一个按钮,执行下一个segue,所以1-> 2-> 3-> 1-> ..等等向前。然后我记录了出现/消失加载和init / deinit方法,但是从未调用deinit。
日志如下所示:
<StoryBoardTest.TestViewController: VC1> init
<StoryBoardTest.TestViewController: VC1> View did load
<StoryBoardTest.TestViewController: VC1> View did appear
<StoryBoardTest.TestViewController: VC2> init
<StoryBoardTest.TestViewController: VC2> View did load
<StoryBoardTest.TestViewController: VC2> View did appear
<StoryBoardTest.TestViewController: VC1> View did disappear
<StoryBoardTest.TestViewController: VC3> init
<StoryBoardTest.TestViewController: VC3> View did load
<StoryBoardTest.TestViewController: VC3> View did appear
<StoryBoardTest.TestViewController: VC2> View did disappear
<StoryBoardTest.TestViewController: VC4> init
<StoryBoardTest.TestViewController: VC4> View did load
<StoryBoardTest.TestViewController: VC4> View did appear
<StoryBoardTest.TestViewController: VC3> View did disappear
<StoryBoardTest.TestViewController: VC5> init
<StoryBoardTest.TestViewController: VC5> View did load
<StoryBoardTest.TestViewController: VC5> View did appear
<StoryBoardTest.TestViewController: VC4> View did disappear
<StoryBoardTest.TestViewController: VC6> init
<StoryBoardTest.TestViewController: VC6> View did load
<StoryBoardTest.TestViewController: VC6> View did appear
<StoryBoardTest.TestViewController: VC5> View did disappear
<StoryBoardTest.TestViewController: VC7> init
<StoryBoardTest.TestViewController: VC7> View did load
<StoryBoardTest.TestViewController: VC7> View did appear
<StoryBoardTest.TestViewController: VC6> View did disappear
如果我将这些嵌入到NavigationController中,这看起来就像我期望的那样,但我不这样做。有人可以阐明为什么这不会消失吗?作为奖励,如果它在NavigationController中会有什么不同。
VC类:
import UIKit
class TestViewController: UIViewController {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print("\(self.description) init")
}
deinit {
print("\(self.description) deinit")
}
override func viewDidLoad() {
super.viewDidLoad()
print("\(self.description) View did load")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("\(self.description) View did appear")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("\(self.description) View did disappear")
}
}