how can I manage controllers in Container View with using tab bar

时光总嘲笑我的痴心妄想 提交于 2019-12-02 16:56:29

问题


On my storyboard I have main ViewController, not TabBarViewController, which consist of TabBar on the bottom, view on the top and ContainerView on the middle. ContainerView have a NavigationController. I also have 4 ViewControllers, one of them - RootViewController of NavigationController. I wish to show one of ViewControllers when I selecting TabBarItem, and in future I will add slide menu, which also will show selected ViewController.
I have next code, which only shows initial ViewController inside ContainerView, and when I selecting TabBarItems, new ViewControllers don't showing and I see only first View Controller. What goes wrong?

class ViewController: UIViewController {

    @IBOutlet weak var container: UIView!
    @IBOutlet weak var first: UITabBarItem!
    @IBOutlet weak var second: UITabBarItem!
    @IBOutlet weak var third: UITabBarItem!
    @IBOutlet weak var fours: UITabBarItem!
    @IBOutlet weak var tabBar: UITabBar!

    var firstVC: FirstViewController?
    var secondVC: SecondViewController?
    var thirdVC: ThirdViewController?
    var foursVC: FoursViewController?

    var navi: UINavigationController?




    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.delegate = self
        initialSetup()
    }

    func initialSetup() {

        tabBar.selectedItem = tabBar.items?.first

        navi = self.storyboard?.instantiateViewController(withIdentifier: "containerNavi") as? UINavigationController

        firstVC = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as? FirstViewController
        secondVC = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
        thirdVC = self.storyboard?.instantiateViewController(withIdentifier: "ThirdViewController") as? ThirdViewController
        foursVC = self.storyboard?.instantiateViewController(withIdentifier: "FoursViewController") as? FoursViewController
    }

    func showVC(number: Int) {
        switch number {
        case 0:
            navi?.popToRootViewController(animated: true)
            print("0")
        case 1:
            if let second = secondVC {
                navi?.pushViewController(second, animated: true)
            }
            print("1")
        case 2:
            if let third = thirdVC {
                navi?.pushViewController(third, animated: true)
            }
            print("2")
        case 3:
            if let fours = foursVC {
                navi?.pushViewController(fours, animated: true)
            }
            print("3")
        default:
            return
        }
    }


}

extension ViewController: UITabBarDelegate {

    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        showVC(number: item.tag)
    }

}

Storyboard screenshot:


回答1:


You can try to use this extesion to add/remove any of the 4 to containerView

extension UIViewController {
    func add(_ child: UIViewController, frame: CGRect? = nil) {
        addChildViewController(child)
        if let frame = frame {
            child.view.frame = frame
        }
        view.addSubview(child.view)
        child.didMove(toParentViewController: self)
    }
    func remove() {
        willMove(toParentViewController: nil)
        view.removeFromSuperview()
        removeFromParentViewController()

   }

}

// use it like this

let vc = self.storyboard?.instantiateViewController(withIdentifier: "first")

self.add(vc, frame: self.containerView.frame)

to remove

vc.remove()


来源:https://stackoverflow.com/questions/49424511/how-can-i-manage-controllers-in-container-view-with-using-tab-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!