从一个 ViewController 到 Tab Bar Controller 中的另一个 ViewController? [英] Segue from one ViewController to another ViewController inside a Tab Bar Controller?

查看:25
本文介绍了从一个 ViewController 到 Tab Bar Controller 中的另一个 ViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在界面构建器中,我有一个 UITabBarController 并且它被设置为初始视图控制器.在标签栏控制器中,我链接了三个独立的 ViewController;两个 UIViewController 和一个 UITableViewController.我已将所有这三个视图都嵌入到 UINavigationController 的内部,因为这些视图中的每一个最终都会切换到一个新视图.

In the interface builder I have a UITabBarController and it is set as the initial view controller. From the tab bar controller, I have linked three independent ViewControllers; two UIViewController's and one UITableViewController. I have embedded all three of these views inside UINavigationController's as each of these views will eventually segue to a new view.

界面生成器

问题:

我现在想使用一个按钮将 UIViewController 的其中一个链接到 UITableViewController 以转到表格视图.这样我就可以将信息,即 func prepareForSegue() 传递给表格视图.我想在底部维护标签栏控制器,但是我不希望能够通过 UITableViewController 从当前的 UITableViewController 返回到以前的 UIViewController>UIBarButtonItem 在视图的顶部;这就是底部标签栏的用途.

I now want to link one of the UIViewController's to the UITableViewController using a button to segue to the table view. This way I can pass information, i.e. func prepareForSegue(), to the table view. 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; 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?

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

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. 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.

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

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.

推荐答案

我使用 segue 来解决它,正如你所说,你仍然会使用 UIViewControllerUINavigationController 看起来有点乱.所以我实际上认为 selectedIndex 可能是最好的方法,因为一旦你切换到 UITableViewController 你就会进入正确的导航堆栈.

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. 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.

与其使用 NSUserDefaults,不如直接从 UIViewController 引用 UITableView 本身,设置所需的值,然后切换到它使用 self.tabBarController.selectedIndex.

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.

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

So for your scenario above it, assuming the UITableViewContollrer is the third view in the UITabBarController, you would do something like the following:

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

  1. Pass whatever you want into the UITabBarController by setting some pre-defined var in it. For example, if there was a String called saveMe in the UITableViewController, then do the following in the UIViewController:

let navController = self.tabBarController?.viewControllers![2] as! UINavigationController
let tableViewController = navController.viewControllers.first as! JarListTableViewController
tableViewController.saveMe = "Saved string here"

  • 切换到 UITableViewController 使用:

    self.tabBarController?.selectedIndex = 2
    

  • 唯一的问题是使用 selectedIndex 不会执行过渡动画,但不确定您是否需要它.如果您这样做,此链接可能会有所帮助.

    The only issue with this is using selectedIndex won't perform a transition animation but not sure if you need this. This link could help if you do.

    这篇关于从一个 ViewController 到 Tab Bar Controller 中的另一个 ViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆