【问题标题】:Best way to deinit a loading view controller?取消加载视图控制器的最佳方法?
【发布时间】:2017-08-09 04:51:15
【问题描述】:

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

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

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

取消初始化第一个视图控制器并使当前视图控制器成为根视图控制器的最佳方法是什么?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

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

    另请参阅this link.

    【讨论】:

    • 即使我添加了 weak var weakSelf = self _ = weakSelf?.popoverPresentationController deinit 也没有被调用
    • 同理,我只是换了self。做popover时使用weakSelf
    【解决方案2】:

    试试这个。

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

    【讨论】:

    • 由于某种原因,navigationController 为 nil,你有什么想法吗?
    • @dmram 也许你没有使用推送?
    • 我不是,我的第一个视图控制器(加载视图控制器)是初始视图控制器
    【解决方案3】:

    如果你 100% 确定 self 永远不会为零,那么只需使用

        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 转换为可选。

    【讨论】:

      猜你喜欢
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 2017-10-06
      相关资源
      最近更新 更多