如何正确解雇作为模态呈现的UINavigationController? [英] How to correctly dismiss a UINavigationController that's presented as a modal?

查看:110
本文介绍了如何正确解雇作为模态呈现的UINavigationController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 TabBarViewController 中,我创建了一个UINavigationController并将其显示为模态。

In my TabBarViewController, I create a UINavigationController and present it as a modal.

var navController =  UINavigationController()
let messageVC = self.storyboard?.instantiateViewControllerWithIdentifier("MessagesViewController") as! MessagesViewController
self.presentViewController(self.navController, animated: false, completion: nil)
self.navController.pushViewController(messageVC, animated: false)

在我的 MessageViewController 中,这就是我想解雇它的方式:

Inside my MessageViewController, this is how I want to dismiss it:

func swipedRightAndUserWantsToDismiss(){
    if self == self.navigationController?.viewControllers[0] {
        self.dismissViewControllerAnimated(true, completion: nil) //doesn't deinit
    }else{
        self.navigationController?.popViewControllerAnimated(true) //deinits correctly
    }
}

deinit{
    print("Deinit MessagesViewController")
}

问题是当我到达根视图控制器并尝试关闭子和UINavigationController时,我的 MessagesViewController deinit不会被调用。有些东西坚持下去 - 很可能是UINavigationController

The problem is that when I get to the root View Controller and try to dismiss both the child and the UINavigationController, my MessagesViewController deinit does not get called. Something's holding on to it -- most likely UINavigationController

推荐答案

您的控制器层次结构如下所示:

Your controller hierarchy looks like this:

UITabViewController
    |
    | presents
    |
UINavigationController
    |
    | contains view controllers
    |
[root, MessagesViewController]

现在,如果你在里面MessagesViewController ,然后它的 navigationController 是一个正在呈现的那个,你应该解雇但是调用 dismiss on MessagesViewController 也应该有用。

Now, if you are inside MessagesViewController, then its navigationController is the one that is being presented and that's the one you should be dismissing but calling dismiss on MessagesViewController should work too.

然而,问题是解雇导航控制器不会删除其视图控制器。您似乎正在使用导航控制器(因为您使用 self.navController 来呈现它)所以状态将变为

However, the problem is that dismissing the navigation controller won't remove its view controllers. It seems you are holding to your navigation controller (since you are presenting it using self.navController) so the state will become

UITabViewController
    |
    | self.navController holds a reference to
    |
UINavigationController
    |
    | contains view controllers
    |
[root, MessagesViewController]

正确销毁 MessagesViewController 你必须要放开 navController ,否则你必须弹出到root(因此删除 MessagesViewController

To properly destroy MessagesViewController you will have to either let go of the navController or you will have to pop to root (thus removing MessagesViewController from view hierarchy).

典型的解决方案是不保存对 navController 的引用。在演示时,您总是可以创建一个新的 UINavigationController
另一个解决方案是使用委托 - 而不是从 MessagesViewController 内部解雇,让它回调给演示者,这将调用

The typical solution would be not to save a reference to navController at all. You could always create a new UINavigationController when presenting. Another solution is using a delegate - instead of dismissing from inside MessagesViewController, let it call back to the presenter, which would call

self.navController.dismiss(animated: true) {
     self.navController = nil
}

这篇关于如何正确解雇作为模态呈现的UINavigationController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆