0

Short explanation.

I have a ContainerViewController that I'm pushing to a navigationStack.

The ContainerViewController has 2 child UIViewController. A SlidePanelViewController (a slide-out menu) and a CenterViewController (the content), my CenterViewController is a UITabBarController which currently has 2 UIViewController.

I have a button in my menu to that I need to change the UIViewController in my tabBarController. However nothing happens when I call my functions.

Here's the function I am trying to call:

func premiumFormulaTapGesture() {
    tabBarController?.selectedIndex = 1
}

(I also tried setting it to 0 and 2. Still with no results.) I tried putting self. in front of it without any luck.

I also tried putting it as a function in my ContainerViewController. But that didn't seem to work either.

Here's how I'm setting up my UITabBarController:

var centerTabBarController: UITabBarController!

in ViewDidLoad():

let tabBarController = UITabBarController()
let suggestionsVC = mySuggestions_VC()
let testVC = detaiLSuggestion_VC()
let controllers = [suggestionsVC,testVC]
tabBarController.setViewControllers(controllers, animated: false)

centerViewController = tabBarController
view.addSubview(tabBarController.view)
addChildViewController(tabBarController)

my tabBarController does show up. And I am able to manually tap the 2 buttons on it, where it switches between the viewControllers as expected. I later plan on hiding the UITabBarController, and use the menu. The UITabBarController is going to be my method of changing UIViewController from the menu.

Also just to clarify. I'm not using storyboards.

Any help changing the viewControllers in my tabBarController would be greatly appreciated!

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
Mark L
  • 759
  • 2
  • 12
  • 29
  • 2
    When you call `tabBarController?.selectedIndex = 1` what is `tabBarController`? (I see that you have that variable name defined locally in `viewDidLoad` but what's it referring to in `premiumFormulaTapGesture`?) – Phillip Mills Apr 04 '15 at 14:54
  • You're completely right. I made a new tabBarController called menuTabBarController, defined it above the class. And changed my functions to menuTabBarController.selectedIndex = 1 and it worked perfectly. Thank you so much. If you want to go ahead and add an answer I'll accept it :) – Mark L Apr 04 '15 at 23:34

1 Answers1

0

Phillip Mills didn't post an answer yet. So I'm just closing the open question.

My problem was that there was 2 UITabBarControllers named the same "tabBarController" after changing the name to "menuTabBarController" everything worked fine.

again thanks to Phillip Mills for solving my issue in the comments.

Mark L
  • 759
  • 2
  • 12
  • 29