![](/img/trans.png)
[英]NotificationCenter.default.removeObserver(self) in deinit, why deinit it even get called?
[英]Why deinit is not called?
這是我的代碼。 我不明白為什么不調用 deinit 。
class SplashVC: UIViewController {
init() {
super.init(nibName: nil, bundle: nil)
print("SplashVC created")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in
self?.navigationController?.popViewController(animated: false)
self?.navigationController?.pushViewController(SignupViewController(), animated: true)
}
}
deinit {
print("SplashVC free")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我閱讀了文檔,現在我知道 pop root view controller 是我的問題。
此方法從堆棧中刪除頂視圖 controller,並使堆棧的新頂部成為活動視圖 controller。 如果堆棧頂部的視圖 controller 是根視圖 controller,則此方法不執行任何操作。 換句話說,您不能彈出堆棧上的最后一項
可能您的SplashVC
是導航 controller 中的根視圖 controller。 如果確實如此,請改用以下代碼:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in
self?.navigationController?.setViewControllers([SignupViewController()], animated: true)
}
}
deinit
方法沒有被調用,因為分配給SplashVC
的 memory 沒有被釋放。
您只是在UINavigationController
UIViewController
UINavigationController
仍然存儲您推送給它的所有視圖控制器,因此仍然分配為每個視圖 controller 分配的 memory
您使用weak self
作為確保沒有 memory 泄漏或保留周期的措施,但在這種情況下,僅當SplashVC
從其UINavigationController
彈出時才會調用deninit
方法。 在您彈出視圖 controller 之前,分配的 memory 仍將保持分配狀態,不能用於其他用途。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.