如何使用选项卡栏在容器视图中管理控制器

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

在我的故事板上,我有主要的 ViewController,而不是 TabBarViewController,它由底部的 TabBar、顶部的视图和中间的 ContainerView 组成. ContainerView 有一个 NavigationController。我还有 4 个 ViewControllers,其中一个 - RootViewControllerNavigationController。我希望在选择 TabBarItem 时显示 ViewControllers 之一,将来我会添加幻灯片菜单,它也会显示选中的 ViewController.
我有下一个代码,它只显示 ContainerView 中的初始 ViewController,当我选择 TabBarItems 时,新的 ViewController 不显示,我只看到第一个视图控制器。出了什么问题?

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)
    }

}

故事板屏幕截图:

您可以尝试将此扩展程序用于 add/remove 4 个容器视图中的任何一个

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()

   }

}

//这样使用

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

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

删除

vc.remove()