Swift:导航栏在以编程方式嵌入标签栏控制器后消失

时间:2015-01-17 22:37:36

标签: ios swift

我刚以编程方式将标签栏控制器嵌入到我的应用程序中,但它似乎强迫我的Storyboard嵌入式导航栏消失,我认为这是因为我现在已将rootviewController设置为我的标签栏

我看了answer to this post,因为问题似乎很相似,但这样做会提示我错误Pushing a navigation controller is not supported

下面是我在AppDelegate中编写的代码,在这里我创建了tab-view和导航控制器以推送到根视图控制器:

    // Set up the tab and navigation bar controllers
    var currController = window?.rootViewController

    let chatSB = UIStoryboard(name: "Chat", bundle: nil)
    let mainSB = UIStoryboard(name: "Main", bundle: nil)

    let tabBarController     = UITabBarController()
    var navigationController = UINavigationController(rootViewController: currController!)

    let profileVC = mainSB.instantiateViewControllerWithIdentifier("profileVC")   as TimelineTableViewController
    let chatVC    = chatSB.instantiateViewControllerWithIdentifier("chatInboxVC") as ChatInboxViewController

    tabBarController.viewControllers = [profileVC, chatVC, navigationController]
    window?.rootViewController = tabBarController

我该如何解决此问题?

1 个答案:

答案 0 :(得分:2)

如果所需的视图控制器嵌入在UINavigationController个实例中,则需要直接实例化那些而不是所需的视图控制器。故事板将负责实例化嵌入式视图控制器。

所以,如果你的两个导航控制器场景有" profileNavController"和" chatInboxNavController"作为他们的标识符,您的代码将是 -

// Set up the tab and navigation bar controllers
    var currController = window?.rootViewController

    let chatSB = UIStoryboard(name: "Chat", bundle: nil)
    let mainSB = UIStoryboard(name: "Main", bundle: nil)

    let tabBarController     = UITabBarController()
    var navigationController = UINavigationController(rootViewController: currController!)

    let profileNavController = mainSB.instantiateViewControllerWithIdentifier("profileNavController")   as UINavigationController
    let chatNavController    = chatSB.instantiateViewControllerWithIdentifier("chatInboxNavController") as UINavigationController

    tabBarController.viewControllers = [profileNavController, chatNavController, navigationController]
    window?.rootViewController = tabBarController