[英]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
,因为这些视图中的每一个最终都会选择到一个新视图。
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
. 我不愿接受只隐藏后退按钮的解决方案,因为当我想从表视图本身导航到详细视图时,我会遇到麻烦,因为我将绕过
UITableViewController
的UINavigationController
。
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来实现它,但是仍然会使用
UIViewController
的UINavigationController
,这似乎有些混乱。 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: 因此,对于上面的场景,假设
UITableViewContollrer
是UITabBarController
的第三个视图,您将执行以下操作:
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
有一个名为saveMe
的String
,则在UIViewController
执行以下操作:
let navController = self.tabBarController?.viewControllers![2] as! UINavigationController let tableViewController = navController.viewControllers.first as! JarListTableViewController tableViewController.saveMe = "Saved string here"
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.