如何在 UITabBarController 中显示 ViewController? [英] How can I show ViewController in UITabBarController?

查看:50
本文介绍了如何在 UITabBarController 中显示 ViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITabBarController 并且我所有的其他视图控制器都连接到它.现在我想将我的控制器显示为:

I have a UITabBarController and all my other view controllers are connected to it. Now I want to show one my controller as:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc: ViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController

但是当我尝试:

let rootViewController = self.window?.rootViewController as! UINavigationController
rootViewController.pushViewController(vc, animated: true)

它给了我下一个错误:

Could not cast value of type 'UITabBarController' (0x1a899b818) to 'UINavigationController'

后来我尝试这样做:

let rootViewController = self.window?.rootViewController as! UITabBarController

但在这种情况下我得到

UITabBar has no member pushViewController

如何显示/推送我的 ViewController 使其与 UINavigationBar 和 UITabBar 一起出现?

How can I show/push my ViewController so it will appear with UINavigationBar and inside of UITabBar?

推荐答案

您需要将每个视图控制器放在一个导航控制器中.

You need to place each of your view controllers inside a navigation controller.

例如目前你有一个 TabBarViewController和两个视图控制器:

E.g. currently you have a TabBarViewController and two view controllers:

  • 视图控制器A
  • 视图控制器B

您需要做的是将它们中的每一个嵌入到导航控制器中,这样您就可以:

What you need to do is to embed each of them inside a navigation controller so you would have:

  • UINavigationController -> ViewControllerA
  • UINavigationController -> ViewControllerB

为了推送一个新的控制器,你会这样做:

In order to push a new controller you would do:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc: ViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController

let navViewController = myTabBar.selectedViewController as? UINavigationController
navViewController?.pushViewController(vc, animated: true)

这篇关于如何在 UITabBarController 中显示 ViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆