Niranjan
Niranjan

Reputation: 13

How to set homeview controller if use a UITabbar in a viewcontroller

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

Upvotes: 0

Views: 154

Answers (1)

HAK
HAK

Reputation: 2071

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.

Upvotes: 1

Related Questions