0

I'm having a hard time presenting a tab bar controller that is not the root view controller.

I have the current setup: enter image description here

I want to press a button in my main view controller and be able to present the tab bar controller with the option to go back to the main view controller.

I tried creating a class of type UITabBarViewController, associating it to my Tab Bar Controller and just presenting it but it does not work.

I would like to present the tab bar controller with the favorites tab selected.

What I tried:

let vc = TabBar()

        self.presentViewController(vc, animated: true, completion: nil)
nhgrif
  • 61,578
  • 25
  • 134
  • 173
Walking
  • 467
  • 1
  • 7
  • 23
  • There's not enough information here. You're using segues. Why don't you just create a modal segue from your main view controller to the tab view controller? – nhgrif Mar 12 '16 at 14:13
  • This took me to the tab bar controller perfectly. Nhgrif, how would I be able to include a button inside my navigation controller that allows the user to end the modal presentation of the tab bar? – Walking Mar 12 '16 at 14:27

2 Answers2

1

You can switch tabs by setting a selected index property of UITabBarController. Like this:

tabBarController.selectedIndex = 1

You don't need to create new view controllers or perform segues if all you want is to switch between the two tabs.

Stefan Salatic
  • 4,513
  • 3
  • 22
  • 30
1

You can do it in tow manner :

using segue :

Drag from button to the tabBarViewController And choose a type (Modal, Push(if your mainViewController is NavBarVC) ...)

from code :

click on your tabBarViewController and go to the attributes inspector and give your VC a storyboard id

and from code :

let mainST = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let VC = mainST.instantiateViewControllerWithIdentifier("idTabBar")
presentViewController(VC, animated: true, completion: nil)

Edit dismiss tabBar

if the tabBar is presented modally, to dismiss it you have tow choices :

1) using a delegate :

protocol ExitMe {
   func exitMe()
}

In the view controller presenter of the tabBar

extension PresenterOfTabBar: ExitMe{
     func exitMe(){
         dismissViewControllerAnimated(false, completion: nil)
     }
}

and in the tabBarViewController define an exitDelegate variable var exitDelegate: ExitMe! and set it's value from the presenter. When the user click a button to exit tabBar you just call exitDelegate.exitMe()

using an unwindFuction when presenting modally using a segue:

in the presenter you define a function like this

@IBAction func unwindFromTabBar(sender: UIStoryboardSegue){
       // do what you want here
}

and in the InterfaceBuilder drag from the the button that should exit the tabbar to the exit in the view controller dock then choose the func unwindFromTabBar.

Others solutions may exist (using notification, get the prsenter View controllers ....) you should pick the suitable one...

LHIOUI
  • 3,287
  • 2
  • 24
  • 37
  • The question needs an update. If you'll read the comments, the use doesn't seem to have difficulty presenting the view controller, but rather dismissing it. – nhgrif Mar 12 '16 at 14:37
  • i didn't read comments but dismissing tabBar is simple as presenting it. – LHIOUI Mar 12 '16 at 14:41