到达TabBarController内的ViewController

时间:2017-03-03 11:58:41

标签: ios iphone swift uiviewcontroller swift3

我正在尝试将菜单应用到我的应用中。我有TabBarViewController -> NavigationViewController -> ContentViewController。当应用程序启动时,我打开HostViewController。在HostViewController内部,我必须使用以下代码:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabBarViewController") as! TabBarViewController
contentList.append(tabbarVC as MenuItemContentViewController)

但是MenuItemContentViewController只继承了UIViewController。所以我不能在tabbar上使用这段代码。

我试过

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let contentVC = storyboard.instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController
contentList.append(contentVC as MenuItemContentViewController)

但是它添加了没有tabbar的contentViewController。

有谁知道如何使用tabbar和navbar添加contentViewController?

感谢。

2 个答案:

答案 0 :(得分:1)

根据我的理解,您希望使用导航栏在tabbar中嵌入contentViewController。您需要在任何事件上推送或添加子视图到hostViewController,即" IBAction或按钮点击"

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabBarViewController") as! TabBarViewController
self.present(tabbarVC, animated: True){} //In HostViewController's Self

或者

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabbarVC = storyboard.instantiateViewController(withIdentifier: "TabBarViewController") as! TabBarViewController 
self.view.addSubview(tabbarVC.view) //In HostViewController's Self

答案 1 :(得分:0)

尝试从UIViewController获取UITabBarController实例 使用

tabbarVC.viewControllers[index] as! MenuItemContentViewController