Abcdefg Abcdefg - 1 year ago 102
Swift Question

Branching off from a tab bar controller?

I've got 3 view controllers. Two I have hooked up to a tab bar controller and one that I'm wanting to access when a user selects a cell on the second view controller in my tabbed views.

I'm wanting when the user hits "back" on the 3rd "detail" page for the user to be taken back to the 2nd view.

When I do this by just adding a button and segueing back to the 2nd VC, the tab bar is gone. I tried in my viewDidAppear to unhide the tab bar, but I guess going off of the tab bar controller messes up the navigation.

I tried creating a variable that was like "didHitBack" and on my "back" button on the 3rd view I'm creating a segue back to the Tab Bar Controller, and if "didHitBack" is true I do

_ self.tabBarController?.selectedIndex = 1

which takes me to the second page, but it's loading the first view in then going to the second page which looks bad.

I was thinking maybe there was a way to do "didHitBack" and setting the tab bar's initial view controller to the second one or something, but that all just seems very wrong.

Is there a "proper" way to do this?

To recap I have VC1 and VC2 that are hooked up to a Tab Bar Controller, I have a tableview on VC2 that on didSelectRow I'm going to VC3 which I do not want to be a part of the tabbed view controller, and when I hit back on VC3 I want to go back to VC2.

Answer Source

If you want to build a navigation stack, you should embed your view controller in a UINavigationController

So your tab bar would be hooked up to VC1 and NavVC. The root view controller of NavVC would be VC2.

You can then push a new view controller onto the stack using the navigation controller (or pop the view controller to go back) all within the confines of the tabBar.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download