3
votes

I successfully added a UITabBar to my UIViewController. But, I'm unable to switch view controllers when tabs are tabbed. I used the following approaches :

public func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

    switch item.tag {
    case 1 :
          var storyboard = UIStoryboard(name: "Main", bundle: nil)
            let viewController = storyboard.instantiateViewController(withIdentifier: "HardwareViewController") as! HardwareViewController
            self.addChildViewController(viewController)
        break
    case 2 :

 self.performSegue(withIdentifier : IDENTIFIER_SEGUE_SYSTEMVC, sender : nil)
        break
    case 3 :

        var storyboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "SystemViewController") as! SystemViewController
        self.view.insertSubview(viewController.view!, belowSubview: self.tabBar)
        break
    case 4 :

        var storyboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "HardwareViewController") as! HardwareViewController
      self.view.insertSubview(viewController.view!, belowSubview: self.tabBar)
        break
    case 5 :

        var storyboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier : "SystemViewController") as! SystemViewController
       self.view.insertSubview(viewController.view!, belowSubview: self.tabBar)
        break
    default:
        break
    }
}

But, none of these worked. Segue presents a new ViewController and tabbar hides.It takes the control all together to a new viewcontroller. I have 2 doubts :

  1. How to show respective viewcontrollers in the same view when respective tab bars are tabbed?
  2. How to load view controller with tabbar with an initial view controller being shown in it? Lets say the view controller to be displayed on first tab should also be shown when view controller with tab bar is loaded.
1

1 Answers

0
votes

The function you are trying public func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) is tabbar delegate method for selected item, you may not use that to set tabbar controller.

You should provide view controllers to your tabbar controller before you load into memory.

try this and see:

let viewController1 = storyboard.instantiateViewController(withIdentifier: "HardwareViewController") as? HardwareViewController
    let viewController2 = storyboard.instantiateViewController(withIdentifier: "SystemViewController") as! SystemViewController
    let viewController3 = storyboard.instantiateViewController(withIdentifier: "HardwareViewController") as! HardwareViewController
    let viewController4 = storyboard.instantiateViewController(withIdentifier : "SystemViewController") as! SystemViewController
    let viewController3 = storyboard.instantiateViewController(withIdentifier: "HardwareViewController") as! HardwareViewController

    var tabbarControllers = [viewController1, viewController2, viewController3, viewController4, viewController5]

   yourtabbarController.viewControllers = tabbarControllers
   // Now load your tabbar controller

Go through this basic tutorial about UITabbarController by Apple. And here is details about viewControllers property for UITabbarController