ios - 如何从另一个处于 segue 中的 View Controller 实例化导航和选项卡 Controller (显示详细信息)?

标签 ios swift uiviewcontroller

我之前问过一个问题,由于其中一个答案 How to instantiate a navigation controller from another view controller?,它成功地工作了,但我遇到了一个新问题,每当我从显示详细信息 segue 中单击一个按钮时,它应该导航到带有导航栏的普通选项卡,但它什么也没做。

这是 Storyboard

enter image description here

场景是这样的

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/

相关文章:

ios - Xcode 11 存档给出 PhaseScriptExecution 失败

ios - Swift 4 UIWebView 视频全屏约束错误

ios - 如何在当前 View Controller 中返回到以前的 View Controller ?

ios - iOS 和 Heroku 上的 FB 平台示例应用 WishList 失败

ios - superview 的 inputViewController 在点击 subview(UITextView) 时被调用

swift - 使用 uiTextView 关闭键盘

ios - 使用 UINavigationController 和 UINavigationBar 的区别

swift - 我的 Segue 或按钮无法快速运行

ios - UISearchDisplayController 配置 "no results" View 不与 tableFooterView 重叠

ios - 触摸屏幕时如何在 Swift 中关闭键盘