【问题标题】:how can I manage controllers in Container View with using tab bar如何使用标签栏管理容器视图中的控制器
【发布时间】:2018-08-31 15:30:14
【问题描述】:

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

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

}

故事板截图:

【问题讨论】:

    标签: ios swift uinavigationcontroller uitabbar uicontainerview


    【解决方案1】:

    您可以尝试使用此扩展将 4 中的任何一个添加/删除到 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()
    
       }
    
    }
    

    // 像这样使用它

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "first")
    
    self.add(vc, frame: self.containerView.frame)
    

    删除

    vc.remove()
    

    【讨论】:

    • 我添加了这个扩展并尝试使用方法: if let second = secondVC, let first = firstVC { first.add(second, frame: second.view.frame) } ,但它没有作品
    • 这段代码会在FirstViewController中吗?我想从主控制器调用和关闭控制器
    • 你应该使用 self.add(first, frame: self.container.frame) 在你在问题中发布它的代码的VC中,而不是从另一个孩子添加一个孩子,您必须先删除旧的孩子,然后才能添加新的孩子
    • 它可以工作,但我有什么容器视图?我会有 UIView 来使用它。
    • 这样用?开关号 { case 0: currentVC?.remove() currentVC = firstVC if let current = currentVC { self.add(current, frame: container.frame) } case 1: currentVC?.remove() currentVC = secondVC if let current = currentVC { self.add(current, frame: container.frame) }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    相关资源
    最近更新 更多