0

I have added a separate UITabbar to a viewcontroller. Made all the necessary outlets. Home viewcontroller has the tabbar in it. What i want it If i click the first button there should be no change but if click the second tabbar item it should show the second screen.

    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem){
        switch item.tag {

        case 1:
            if sampleOne == nil {

                var storyboard = UIStoryboard(name: "Main", bundle: nil)

                sampleOne = storyboard.instantiateViewController(withIdentifier: "s1") as! SampleOneViewController
            }
            self.view.insertSubview(sampleOne!.view!, belowSubview: self.sampleTabBar)

            break


        case 2:
            if sampleTwo == nil {

                var storyboard = UIStoryboard(name: "Main", bundle: nil)

                sampleTwo = storyboard.instantiateViewController(withIdentifier: "s2") as! SampleTwoViewController
            }
            self.view.insertSubview(sampleTwo!.view!, belowSubview: self.sampleTabBar)

            break

        default:

            break

        }

    But it loads Homeviewcontroller first then it shows the other viewcontroller. 
    Now how should i set the homeviewcontroller(which has uitabbar in it) as first viewcontroller. 

For Anbu's help? enter image description here

Dinsen
  • 2,139
  • 3
  • 19
  • 25
Niranjan
  • 13
  • 7

1 Answers1

1

According to the documentation if you want to switch between different views while keeping the viewcontroller same, use UITabBar. But if you want to switch between different viewcontrollers, UITabBarController should be the preferred way to go.

The problem you might face while using UITabBar to switch viewcontrollers is that you need to manually handle a lot of things. E.g. Adding, and removing your child viewcontrollers.

But if you still insist to do so use a parent child relationship between your viewcontrollers. Make your HomeViewController the parent view. Now on viewDidLoad assuming that first item is selected by default, add SampleOneViewController like this:

        if let vc = self.storyboard?.instantiateViewController(withIdentifier: "s1") as? SampleOneViewController {
        self.addChildViewController(vc)
        self.view.insertSubview(vc, belowSubview: tabBar)
    }

Now in the tabbar delegate you need to remove the previous child viewcontrollers first before adding the one selected by the index.

So your delegate method will become something like this:

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem){
    switch item.tag {

// Remove the previous child viewcontrollers
    for vc in self.childViewControllers {
        vc.willMove(toParentViewController: nil)
        vc.view.removeFromSuperview()
        vc.removeFromParentViewController()
    }


    case 1:

    if let vc = self.storyboard?.instantiateViewController(withIdentifier: "s1") as? SampleOneViewController {
        self.addChildViewController(vc)
        self.view.insertSubview(vc, belowSubview: self.view)
    }

        break


    case 2:
    if let vc = self.storyboard?.instantiateViewController(withIdentifier: "s2") as? SampleTwoViewController {
        self.addChildViewController(vc)
        self.view.insertSubview(vc, belowSubview: self.view)
    }

        break

    default:

        break

    }

Hope this helps.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
HAK
  • 2,023
  • 19
  • 26
  • Can add the views but the objects like button and label in homeviewcontrollers are showing up in all views. – Niranjan Oct 29 '18 at 07:02
  • That is because you are using insertSubviews method. I think you have to add a separate UIView in HomeViewController, create its outlet and then instead of inserting the views in self.view, add them in that separate view making sure that no other button or label obscures it. – HAK Oct 29 '18 at 07:24
  • Actually i am adding the other views belowsubview: "self.sample"! – Niranjan Oct 29 '18 at 07:36
  • Check the view hierarchy of your HomeViewController in the storyboard. If there is anything that is above the sampleTabBar it will be displayed on all the other views because you are adding your other views below the sampleTabBar. – HAK Oct 29 '18 at 07:39
  • 1
    Thank you HAK! A very simple issue but took more time to fix. Thank you that the reordering helped. – Niranjan Oct 29 '18 at 10:31