如何实例化一个导航和选项卡控制器从其他视图控制器是在继续(显示详细信息)?

我之前问过一个问题,它成功的工作得益于其中一个答案如何从另一个视图控制器实例化导航控制器? ,但我遇到了一个新的问题,即每当我点击一个button从显示细节赛格,它应该导航到一个导航栏的正常选项卡,但它什么都没做。

这是故事板

在这里输入图像说明

这是场景

1)用户点击firstViewController上的一个button,它将继续显示到第三个显示细节的ViewController

2)用户点击另一个button,然后应该使用下面的代码去第二个ViewController

这是代码

在ThirdViewController中

@IBAction func buttonTapped(sender: UIButton) { guard let tabBarController = tabBarController else { return } let navController = tabBarController.viewControllers![1] as! UINavigationController let secondViewController = navController.topViewController as! SecondViewController secondViewController.name = "My name is TDog" tabBarController.selectedIndex = 1 } 

我做错了什么? 我还需要实例化吗?

你必须closures实际的ThirdViewController但是在这个类中,你还不知道UITabViewController是如何获得的(不是重新实例化,而是从内存中调用)是调用你的窗口的rootViewController(它可以在你的项目):

 @IBAction func buttonTapped(sender: AnyObject) { self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil) let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let tabBarController = appDelegate.window!.rootViewController if tabBarController is UITabBarController { let tab = tabBarController as! UITabBarController let navController = tab.viewControllers![1] as! UINavigationController let secondViewController = navController.topViewController as! SecondViewController secondViewController.nameString = "My name is TDog" tab.selectedIndex = 1 } } 

你必须打电话

 self.presentingViewController.dismissViewControllerAnimated(true, completion: nil) 

之后的行

 tabBarController.selectedIndex = 1