Swift 3:popToViewController不起作用 [英] Swift 3: popToViewController not working

查看:108
本文介绍了Swift 3:popToViewController不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有三个表视图控制器,然后有很多UIViewController,如果用户在任何时候按回去,每个UIViewController都必须引回到第一个表视图控制器.我不希望用户必须翻阅潜在的数百页.这就是我要确定用户是否按了后退按钮,并且可以正常打印邮件的信息

In my app I have three table view controllers and then potentially many UIViewControllers each of which has to lead back to the first table view controller if the user presses back at any point. I don't want the user to have to back through potentially hundreds of pages. This is what I amusing to determine if the user pressed the back button and it works the message is printed

override func viewWillDisappear(_ animated: Bool) {
    if !movingForward {
        print("moving back")
        let startvc = self.storyboard!.instantiateViewController(withIdentifier: "FirstTableViewController")
        _ = self.navigationController!.popToViewController(startvc, animated: true)
    }
}

我进行了搜索,到目前为止,所有解决方案都没有奏效.

I have searched and none of the solutions have worked so far.

推荐答案

popToViewController不能以某种方式工作,您尝试传递的是FirstTableViewController的完整新引用,而不是导航堆栈中的引用.因此,您需要遍历navigationController?.viewControllers并找到FirstTableViewController,然后使用FirstTableViewController的实例调用popToViewController.

popToViewController not work in a way you are trying you are passing a complete new reference of FirstTableViewController instead of the one that is in the navigation stack. So you need to loop through the navigationController?.viewControllers and find the FirstTableViewController and then call popToViewController with that instance of FirstTableViewController.

for vc in (self.navigationController?.viewControllers ?? []) {
    if vc is FirstTableViewController {
        _ = self.navigationController?.popToViewController(vc, animated: true)
        break
    }
}

如果您想移至第一屏幕,则可能需要使用popToRootViewController而不是popToViewController.

If you want to move to First Screen then you probably looking for popToRootViewController instead of popToViewController.

_ = self.navigationController?.popToRootViewController(animated: true)

这篇关于Swift 3:popToViewController不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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