取消加载视图控制器的最佳方法是什么?

时间:2017-08-09 04:51:15

标签: ios swift

当我的应用程序启动时,我有一个加载视图控制器,当此视图控制器中的动画完成时,我希望它显示另一个视图控制器并使用动画关闭视图控制器。

加载视图控制器是初始视图控制器,

我在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")
}

取消第一个视图控制器,并使呈现视图控制器成为根视图控制器的最佳方法是什么?

3 个答案:

答案 0 :(得分:0)

当您使用弱引用周期时deinit方法正在调用。在强引用周期deinit中没有调用。因此,为调用方法创建弱引用循环。

另请参阅this link.

答案 1 :(得分:0)

试试这个。

使用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)

答案 2 :(得分:0)

如果您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
            })

        }
 }`

weakunowned是相同的。除了一件事。与我们看到的weak不同,unowned不会在闭包块中自动将self转换为可选项。