0

I have just programatically embedded a tab bar controller into my app, however it seems to have forced my Storyboard embedded Navigation Bars to disappear, I assume this is because I have now set the rootviewController to be my tab bar.

I read the answer to this post as it seemed that the problem was similar, however doing so prompts me with the error Pushing a navigation controller is not supported

Below is my code written in AppDelegate, here I create my tab-view and a navigation controller to push to the root view controller:

    // 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

How would I go about fixing this issue?

Community
  • 1
  • 1
Halfpint
  • 3,967
  • 9
  • 50
  • 92
  • Is the navigation controller "outside" of the tab or do you VCs in the tabs have navigation controllers? – Paulw11 Jan 17 '15 at 22:40
  • The navigation controllers are internal, for example Profile and Chat each have their own internal navigation stack on their Storyboards. – Halfpint Jan 17 '15 at 22:42
  • Then instead of instantiating the `TimelineTableViewController` and `ChatInboxViewController` VCs you should instantiate the UINavigationControllers in which they are embedded and add those to your tab bar controller. The storyboard will take care of instantiating the timeline & chat VCs that are embedded in the navigation controllers – Paulw11 Jan 17 '15 at 22:44
  • Could you possibly give me an example of that as an answer? Sounds promising - first time I've had to build my nav stack not using the storyboard, I think I've taken it for granted in the past – Halfpint Jan 17 '15 at 22:44

1 Answers1

2

If your desired view controllers are embedded in UINavigationController instances you need to instantiate those rather than the desired view controllers directly. The storyboard will take care of instantiating the embedded view controllers.

So, if your two navigation controller scenes have "profileNavController" and "chatInboxNavController" as their identifiers, your code would be -

// 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
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • That worked perfectly after I removed the `navigationController` and `currController` variables - that kept throwing the `Pushing a navigation controller is not supported`. Thanks Paul. – Halfpint Jan 17 '15 at 22:52