在标签栏控制器中从一个 ViewController 切换到另一个 ViewController?

Segue from one ViewController to another ViewController inside a Tab Bar Controller?

在界面生成器中我有一个 UITabBarController 并且它被设置为初始视图控制器。在选项卡栏控制器中,我 link 编辑了三个独立的 ViewController;两个 UIViewController 和一个 UITableViewController。我已将所有这三个视图嵌入到 UINavigationController 中,因为这些视图中的每一个最终都会切换到一个新视图。

Interface Builder

问题:

我现在想 link UIViewController 之一到 UITableViewController 使用按钮转到 table 视图。这样我就可以将信息(即 func prepareForSegue())传递给 table 视图。我想在底部保留标签栏控制器,但是我不想通过 UIBarButtonItem 从当前 UITableViewController 返回到之前的 UIViewController视图的顶部;这就是底部标签栏的用途。

但是每次我转"Show" table 视图(它实际上是 table 视图导航控制器的转转),顶部和底部的导航栏table 视图消失。有什么办法可以防止这种情况发生吗?

我也试过将 "Show" 直接转至 table 视图,在这种情况下,标签栏是可见的,但随后它会在顶部显示一个 "back" 按钮查看继续发送 UIViewController。我对接受一个只隐藏后退按钮的解决方案犹豫不决,因为我觉得当我想从 table 视图本身导航到详细视图时,我会 运行 遇到问题,因为我会绕过 UITableViewControllerUINavigationController

任何解决方案将不胜感激。几个小时以来,我一直在努力解决这个问题,我正要把头伸到电脑屏幕上。我还考虑过在单击按钮时仅使用 tabBarController?.selectedIndex 以切换到 table 视图,然后使用 NSUserDefaults 传递信息,但这是不可能的,因为我会传递自定义对象,并且必须对每个自定义字段进行编码和解码。

我使用 segue 来获取它,如您所说,您仍将使用 UIViewControllerUINavigationController,这看起来有点混乱。所以我实际上认为 selectedIndex 可能是最好的方式,因为一旦您更改为 UITableViewController,您就会进入正确的导航堆栈。

而不是使用 NSUserDefaults,为什么不直接从 UIViewController 引用 UITableView 本身,设置你想要的值,然后使用 self.tabBarController.selectedIndex 交换到它.

因此对于上面的场景,假设 UITableViewContollrerUITabBarController 中的第三个视图,您将执行如下操作:

  1. 通过在其中设置一些预定义的 var,将您想要的任何内容传递到 UITabBarController。例如,如果 UITableViewController 中有一个名为 saveMeString,则在 UIViewController 中执行以下操作:

    let navController = self.tabBarController?.viewControllers![2] as! UINavigationController
    let tableViewController = navController.viewControllers.first as! JarListTableViewController
    tableViewController.saveMe = "Saved string here"
    
  2. 交换到 UITableViewController 使用:

    self.tabBarController?.selectedIndex = 2
    

唯一的问题是使用 selectedIndex 不会执行过渡动画,但不确定您是否需要它。 This link could help if you do.