我之前问过一个问题,由于其中一个答案 How to instantiate a navigation controller from another view controller?,它成功地工作了,但我遇到了一个新问题,每当我从显示详细信息 segue 中单击一个按钮时,它应该导航到带有导航栏的普通选项卡,但它什么也没做。
这是 Storyboard
场景是这样的
1) 用户点击 firstViewController 上的按钮,它会跳转至显示详细信息的 thirdViewController
2) 用户点击另一个按钮,然后应该使用下面的代码转到第二个 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
}
我做错了什么?我还需要实例化吗?
最佳答案
你必须关闭实际的 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
}
}
关于ios - 如何从另一个处于 segue 中的 View Controller 实例化导航和选项卡 Controller (显示详细信息)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37997546/