我在另一个 parentVC(vc1) 中的 parentVC(vc2) 中有一个 childVC(vc3)。我这样做是出于动画目的。
发生的事情是我将 vc3 作为孩子添加到 vc2。我有一个推动 vc1 的 collectionView。一旦 vc1 出现在场景中,就会将 vc2 添加到其中。当我将 vc1 从堆栈中弹出并返回到 collectionView 时,vc1 中的 deinit 会被调用,但是 vc2 中的 deinit 永远不会被调用。
vc2 中的 deinit 是否应该被调用,即使它是 vc1 的孩子?或者可能是因为 thirdVC 在 secondVC 内部创建了对自身的强引用?
在其中添加了 ThirdVC 的 SecondVC:
class SecondController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let thirdVC = ThirdController()
addChildViewController(thirdVC)
view.addSubview(thirdVC.view)
thirdVC.didMove(toParentViewController: self)
}
// this never runs when the firstVC is popped off the stack
deinit{
print("the secondVC has be deallocated")
}
}
集合视图:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let firstVC = FirstController()
navigationController?.pushViewController(firstVC, animated: true)
}
第一VC:
class FirstController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let secondVC = SecondController()
addChildViewController(secondVC)
view.addSubview(secondVC.view)
secondVC.didMove(toParentViewController: self)
}
// this runs when popped off the stack
deinit{
print("the firstVC has be deallocated")
}
}