执行 segue 以查看选项卡和导航控制器后面的控制器

perform segue to view controller behind tab and nav-controller

我在访问位于 TAB 和 NAV 控制器后面的视图控制器时遇到问题。

我可以访问第一个 VC(请参阅下面的代码)但我无法访问第四个 VC。

         SenderVC----->TabBarVC-⎜----NavVC----FirstVC
                                ⎜----NavVC----SecondVC
                                ⎜----NavVC----ThirdVC
                                ⎜----NavVC----FourthVC

我做错了什么?

如果有人能指出正确的方向,那就太好了。谢谢。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if (segue.identifier == "segueVC0") {
        let tabVC = segue.destination as! UITabBarController
        let navVC = tabVC.viewControllers![0] as! UINavigationController
        let destVC = navVC.viewControllers[0] as! FirstVC  // ==> this transition is working

    } else if (segue.identifier == "segueVC4") {
        let tabVC = segue.destination as! UITabBarController
        let navVC = tabVC.viewControllers![4] as! UINavigationController
        let destVC = navVC.viewControllers[0] as! FourthVC  // ==> this transition is NOT working !!!

    } else {
        print ("wrong segue ID")
    }
}

您好,为 UITabBarController 创建自定义 class,然后像这样获取目的地

let tabVC = segue.destination as! HomeTabBarcController
  let navVC = tabVC.viewControllers![4] as! UINavigationController
  let destVC = navVC.topViewController as! FourthVC

对我有用。希望对你有用

您好,我得到了第四个 VC

的实例

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func buttonTapped(sender: AnyObject) {
    print("ViewController - buttonTapped()")
    performSegue(withIdentifier: "seg4", sender: self)
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "seg1" {
        let tabVC = segue.destination as! UITabBarController
        let navVC = tabVC.viewControllers![0] as! UINavigationController
        let destVC = navVC.viewControllers[0] as! Seq1  // ==> this transition is working

        print(destVC)

    }
    else if segue.identifier == "seg4"{
        let tabVC = segue.destination as! UITabBarController
        let navVC = tabVC.viewControllers![3] as! UINavigationController
        let destVC = navVC.viewControllers[0] as! Seq4  // ==> this transition is working
        destVC.hello()
        print(destVC)
    }

}

}

class Seq1: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

class Seq2: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

} class Seq3: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

} class Seq4: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    print("Hello")
    // Do any additional setup after loading the view, typically from a nib.
}

func hello() {
    print("Hello")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

注意:标签栏控制器将始终显示第一个VC。因为它是默认选择。如果需要,更改 selectedIndex = 3