[英]Best way to deinit a loading view controller?
我的應用啟動時,我有一個正在加載的視圖控制器,當該視圖控制器中的動畫完成時,我希望它顯示另一個視圖控制器,並用動畫關閉該視圖控制器。
加載視圖控制器是初始視圖控制器,
UIStoryboard.mflMainTabBarViewController()時有此代碼。 返回我要呈現的視圖控制器
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
let animationID = anim.value(forKey: "animationID")
if animationID as! NSString == "transform" {
self.present(UIStoryboard.mflMainTabBarViewController(), animated: true, completion: {
_ = self.popoverPresentationController
})
}
}`
但是當從未調用deinit時
deinit {
print("deinit")
}
取消初始化第一個視圖控制器並使呈現視圖控制器成為根視圖控制器的最佳方法是什么?
當您當時使用弱引用循環時,將調用deinit
方法。 在強引用循環中, deinit
不調用。 因此,創建弱引用循環來調用該方法。
另請參閱此鏈接。
嘗試這個。
使用vc的父項進行呈現。
並關閉vc本身。
self.presentingViewController?.present(UIStoryboard.mflMainTabBarViewController(), animated: true, completion: {
_ = self.popoverPresentationController
})
self.dismiss(animated: false, completion: nil)
要么
self.navigationController?.present(UIStoryboard.mflMainTabBarViewController(), animated: true, completion: {
_ = self.popoverPresentationController
})
self.navigationController?.popViewController(animated: false)
如果您100%確定自己永遠不會為零,那么只需使用
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
let animationID = anim.value(forKey: "animationID")
if animationID as! NSString == "transform" {
self.present(UIStoryboard.mflMainTabBarViewController(), animated: true, completion: { [unowned self] in
_ = self.popoverPresentationController
})
}
}`
weak
和unowned
者是一樣的。 除了一件事。 與我們已經看到的weak
不同,在閉包塊內, unowned
不會自動將self
轉換為可選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.