简体   繁体   English

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

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

In the interface builder I have a UITabBarController and it is set as the initial view controller. 在界面构建器中,我有一个UITabBarController并将其设置为初始视图控制器。 From the tab bar controller, I have linked three independent ViewControllers; 从标签栏控制器,我链接了三个独立的ViewController; two UIViewController 's and one UITableViewController . 两个UIViewController和一个UITableViewController I have embedded all three of these views inside UINavigationController 's as each of these views will eventually segue to a new view. 我将所有这三个视图都嵌入了UINavigationController ,因为这些视图中的每一个最终都会选择到一个新视图。

Interface Builder 界面生成器

Problem: 问题:

I now want to link one of the UIViewController 's to the UITableViewController using a button to segue to the table view. 我现在想使用一个按钮将一个UIViewController链接到UITableViewController ,以选择到表视图。 This way I can pass information, ie func prepareForSegue() , to the table view. 这样,我可以将信息(即func prepareForSegue()传递到表视图。 I want to maintain the tab bar controller at the bottom, however I do not want to have the ability to go back to the previous UIViewController from the current UITableViewController via a UIBarButtonItem at the the top of the view; 我想将标签栏控制器保持在底部,但是我不希望能够通过视图顶部的UIBarButtonItem从当前的UITableViewController返回到以前的UIViewController That is what the tab bar at the bottom is for. 这就是底部的标签栏的作用。

However every time I segue "Show" the table view (it is actually a segue to the table views navigation controller), the navigation bars at the top and the bottom of the table view disappear. 但是,每次我选择“显示”表视图时(实际上是对表视图导航控制器的选择),表视图顶部和底部的导航栏都会消失。 Is there anyway to prevent this from happening? 是否有防止这种情况发生的方法?

I have also tried segue "Show" directly to the table view, in which case the tab bar is visible, but then it displays a "back" button at the top of the view to segue back to the sending UIViewController . 我也尝试过直接将“ Show”设置为表格视图,在这种情况下,标签栏可见,但是随后它在视图顶部显示了一个“ back”按钮,以将其设置为发送方UIViewController I am hesitant about accepting a solution that would just hide the back button, because I feel I will run into problems down the road when I want to navigate to a detail view from the table view itself, since I would be bypassing the UITableViewController 's UINavigationController . 我不愿接受只隐藏后退按钮的解决方案,因为当我想从表视图本身导航到详细视图时,我会遇到麻烦,因为我将绕过UITableViewControllerUINavigationController

Any solutions would be greatly appreciated. 任何解决方案将不胜感激。 I have been trying to solve this problem for hours and I'm about to put my head through my computer screen. 数小时以来,我一直在试图解决这个问题,而我正要把头放在电脑屏幕上。 Also I thought about just using tabBarController?.selectedIndex on the button click to shift to the table view, and then passing the information using NSUserDefaults , but this is out of the question since I would be passing a custom object, and would have to encode and decode every custom field. 我还考虑过只单击按钮上的tabBarController?.selectedIndex即可切换到表格视图,然后使用NSUserDefaults传递信息,但这是NSUserDefaults ,因为我将传递自定义对象,并且必须进行编码并解码每个自定义字段。

I you use a segue to get to it, as you say, you will still be using the UIViewController 's UINavigationController which seems a bit messy. 正如您所说,我使用segue来实现它,但是仍然会使用UIViewControllerUINavigationController ,这似乎有些混乱。 So I actually think selectedIndex is probably the best way to go as once you change to the UITableViewController you'll be in the correct navigation stack. 所以我实际上认为selectedIndex可能是最好的selectedIndex ,因为一旦更改为UITableViewController您将进入正确的导航堆栈。

Instead of using NSUserDefaults , why not just reference the UITableView itself from the UIViewController , set the values you want, and then swap to it using self.tabBarController.selectedIndex . 除了使用NSUserDefaults ,为什么不直接从UIViewController引用UITableView本身,设置所需的值,然后使用self.tabBarController.selectedIndex交换给它。

So for your scenario above it, assuming the UITableViewContollrer is the third view in the UITabBarController , you would do something like the following: 因此,对于上面的场景,假设UITableViewContollrerUITabBarController的第三个视图,您将执行以下操作:

  1. Pass whatever you want into the UITabBarController by setting some pre-defined var in it. 通过在其中设置一些预定义的var ,将所需的任何内容传递给UITabBarController For example, if there was a String called saveMe in the UITableViewController , then do the following in the UIViewController : 例如,如果UITableViewController有一个名为saveMeString ,则在UIViewController执行以下操作:

     let navController = self.tabBarController?.viewControllers![2] as! UINavigationController let tableViewController = navController.viewControllers.first as! JarListTableViewController tableViewController.saveMe = "Saved string here" 
  2. Swap to the UITableViewController using: 使用以下方法交换到UITableViewController

     self.tabBarController?.selectedIndex = 2 

The only issue with this is using selectedIndex won't perform a transition animation but not sure if you need this. 唯一的问题是使用selectedIndex不会执行过渡动画,但不确定是否需要此动画。 This link could help if you do. 如果您这样做,此链接可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Segue无法从标签栏控制器中的ViewController运行 - Segue not working from the viewcontroller inside tabbar controller 一个选项卡栏控制器的所有选项卡的一个ViewController - one ViewController for all tabs of Tab Bar Controller 如何从一个视图控制器转移到另一个视图控制器 - How to segue from one viewcontroller to another 将数据从标签栏 controller 传输到 ViewController - Transfer data from tab bar controller to ViewController 通过Segue返回时,选项卡栏从viewController中消失-Swift - Tab bar vanishes from a viewController when returned to via a segue - swift 查找数据并将数据从ViewController传输到另一个ViewController - segue and transfer data from viewcontroller to viewcontroller to another viewcontroller 从UITableViewCell到另一个ViewController - Segue from UITableViewCell to another ViewController 当从一个ViewController到另一个ViewController执行show segue时,导航栏消失了? - Navigation bar disappears when show segue is executed from one ViewController to another? 从一个viewController到另一个Tab上的另一个 - Going from one viewController to another on a different Tab 如何在标签栏控制器中从一个视图控制器切换到另一个视图控制器并保留标签栏? - How within a tab bar controller do I segue from one view controller to another and retain the tab bar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM