问题
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.
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. 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?
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
.
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.
回答1:
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.
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
.
So for your scenario above it, assuming the UITableViewContollrer
is the third view in the UITabBarController
, you would do something like the following:
Pass whatever you want into the
UITabBarController
by setting some pre-definedvar
in it. For example, if there was aString
calledsaveMe
in theUITableViewController
, then do the following in theUIViewController
: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: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. This link could help if you do.
来源:https://stackoverflow.com/questions/33091131/segue-from-one-viewcontroller-to-another-viewcontroller-inside-a-tab-bar-control