在导航堆栈中跳过/添加视图控制器 [英] Skip/Add a View Controller in Navigation Stack

查看:144
本文介绍了在导航堆栈中跳过/添加视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在导航控制器中嵌入了三个视图控制器。我想从VC1转到VC3,这样在VC3中导航项的后退按钮会将用户引导到VC2而不是VC1。我认为这应该通过在创建VC3时将VC2添加到VC1和VC3之间的导航堆栈或通过跳过第二个View Controller 来完成。

I have three View Controllers embedded in Navigation Controller. I want to go from VC1 to VC3 so that in VC3 the Navigation Item's back button would direct the user to VC2 instead of VC1. I think this should be done either by adding the VC2 to the Navigation Stack between VC1 and VC3 when VC3 is created or by skipping the second View Controller.

我试过这个:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier {
        switch identifier {
        case "JumpToThirdVCSegue":
            if let secondvc = segue.destinationViewController as? SecondViewController {
                secondvc.performSegueWithIdentifier("ThirdVCSegue", sender: self)
            }
        default: break
        }
    }
}

但我无法正常工作。当View Controller尚未打开时,可能无法执行segue?

but I couldn't get it working. Perhaps performing a segue isn't possible when the View Controller isn't open yet?

跳过视图控制器/添加视图控制器的最佳方法是什么?导航堆栈的中间?我希望这可以帮助你理解我想要做的事情:

What is the best way to skip a View Controller / add a View Controller in the middle of Navigation Stack? I hope this helps you to understand what I'm trying to do:

推荐答案

这样的事情应该有效:

    self.navigationController?.pushViewController(viewController2, animated: true)
    self.navigationController?.pushViewController(viewController3, animated: true)

编辑:

如果要在不被用户注意的情况下推送第二个视图控制器,则需要将其添加到导航控制器中推送第三个视图控制器。这可以通过实现 UINavigationControllerDelegate 来完成。您可以将第二个视图控制器存储在变量中,并将其插入到委托方法中的导航控制器层次结构中。您的主视图控制器将如下所示:

If you want to push the second view controller without being noticed by the user you need to add it to the navigation controller after the third view controller is pushed. This can be done by implementing UINavigationControllerDelegate. You can store your second view controller in a variable and insert it to the navigation controllers hierarchy in delegate method. Your main view controller will look like following:

class MyViewController: UIViewController, UINavigationControllerDelegate {

    var viewControllerToInsertBelow : UIViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.delegate = self
    }

    func pushTwoViewControllers() {
        if let viewController2 = self.storyboard?.instantiateViewControllerWithIdentifier("id1"),
            let viewController3 = self.storyboard?.instantiateViewControllerWithIdentifier("id2") { //change this to your identifiers
                self.viewControllerToInsertBelow = viewController2
                self.navigationController?.pushViewController(viewController3, animated: true)
        }
    }

    //MARK: - UINavigationControllerDelegate
    func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
        if let vc = viewControllerToInsertBelow {
            viewControllerToInsertBelow = nil
            let index = navigationController.viewControllers.indexOf(viewController)!
            navigationController.viewControllers.insert(vc, atIndex: index)
        }
    }

}

这篇关于在导航堆栈中跳过/添加视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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