访问嵌入在UIViewController中的UITabBarController

时间:2019-03-23 18:14:38

标签: ios swift uiviewcontroller storyboard uitabbarcontroller

我在UIViewController中嵌入了一个UITabBarController。我正在尝试从父UIViewController访问UITabBarController实例。

vc.children的返回类型为[UIViewController],因此不会显示UITabBarController。

vc.tabBarController为nil,因为该视图未嵌入在TabBarController中。我的设置是另一种方式。

有什么想法吗?

我的故事板: storyboard

2 个答案:

答案 0 :(得分:0)

最快的解决方案:
vc.children.compactMap({$0 as? UITabBarController}).first

最佳解决方案:
从情节提要中选择Embed Segue,然后指定一个标识符(例如“ containerEmbedSegue”。接下来,在您的vc中:

var tabBarVC: UITabBarController?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "containerEmbedSegue" {
        self.tabBarVC = segue.destination as? UITabBarController
    }
}

答案 1 :(得分:0)

我需要将window?.rootViewController = viewController移到vc.children之前。