单个容器视图中的多个子viewControllers

时间:2016-05-07 14:22:17

标签: ios swift uicontainerview

我是 Swift 的新手,我必须在一个UIContainerView中使用故事板布局管理多个子视图控制器,但容器视图只能一次提供一个子视图。

我已使用以下代码:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
var controller: UIViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC") as UIViewController
self.containerViewA.addSubview(controller.view)
self.addChildViewController(controller)

1 个答案:

答案 0 :(得分:1)

在容器视图的ios中,只能连接一个子控制器"一次" ,而不是子视图控制器,我们可以使用一个带有RootviewControlle的UINavigationController,我们可以使用多个UINavigationController加载多个子视图控制器,如下所述。

var navController1 = UINavigationController()
var navController2 = UINavigationController()
var navController3 = UINavigationController()
var navController4 = UINavigationController()

override func viewDidLoad()
{
    super.viewDidLoad()

    //navController1
    let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("HomeVC") as! HomeVC
    navController1 = UINavigationController(rootViewController: VC1)
    navController1.navigationBarHidden = true

    //navController2
    let VC2 = self.storyboard!.instantiateViewControllerWithIdentifier("RoomControlesVC") as! RoomControlesVC
    navController2 = UINavigationController(rootViewController: VC2)
    navController2.navigationBarHidden = true

    //navController3
    let VC3 = self.storyboard!.instantiateViewControllerWithIdentifier("TelevisionVC") as! TelevisionVC
    navController3 = UINavigationController(rootViewController: VC3)
    navController3.navigationBarHidden = true

    //navController4
    let VC4 = self.storyboard!.instantiateViewControllerWithIdentifier("EntertainmentVC") as! EntertainmentVC
    navController4 = UINavigationController(rootViewController: VC4)
    navController4.navigationBarHidden = true

    let btn = UIButton()
    btn.tag = 1
    self.ActionOnSideMenuButtons(btn)
}

@IBAction func ActionOnSideMenuButtons(sender: AnyObject)
{
    for view in containerViewA.subviews{
        view.removeFromSuperview()
    }
    switch sender.tag
    {
    case 1:// Home

        NSLog("Home Button tapped")
        addChildViewController(self.navController1)
        self.navController1.view.frame = self.containerViewA.bounds
        self.containerViewA.addSubview(navController1.view)
        break

    case 2:// Room Controles
        NSLog(" Room Controles Button tapped")
        self.navController2.view.frame = self.containerViewA.bounds
        self.containerViewA.addSubview(navController2.view)
        break
    case 3:// Television
        NSLog("Television Button tapped")
        self.navController3.view.frame = self.containerViewA.bounds
        self.containerViewA.addSubview(navController3.view)
        break
    case 4:// Entertainment
        NSLog("Entertainment Button tapped")
        self.navController4.view.frame = self.containerViewA.bounds
        self.containerViewA.addSubview(navController4.view)
        break

    default:
        break
    }

}

在上面的代码中,我创建了4个导航控制器并在viewDidLoad方法&中初始化它们。 on" ActionOnSideMenuButtons"方法我只要点击任何侧面菜单按钮就加载子视图控制器。