1
votes

I need to go programmatically to a viewcontroller from another Storyboard. From here easy. But the problem is: that viewController is embedded in a navigation controller, and this one into a tabBarController (see image)

enter image description here

I've tried:

    let storyboard = UIStoryboard(name: "Inside", bundle: nil)

    let viewcontroller = storyboard.instantiateViewController(withIdentifier: "controller_id")
    let navigationController = UINavigationController(rootViewController: viewcontroller)

    self.present(navigationController, animated: true, completion: nil)

What I get with this is: I go to the correct controller, with navigation controller working, but no tabbarcontroller

Another try:

    let storyboard = UIStoryboard(name: "Inside", bundle: nil)

    let tabbar = storyboard.instantiateViewController(withIdentifier: "tab_id")
    let navigationController = UINavigationController(rootViewController: tabbar)

    self.present(navigationController, animated: true, completion: nil)

In this case, tabbar is working, but no navigationcontroller.

And... try number 3:

    let storyboard = UIStoryboard(name: "Inside", bundle: nil)
    let tabbar = storyboard.instantiateViewController(withIdentifier: "tab_id")
    let viewcontroller = storyboard.instantiateViewController(withIdentifier: "race_id")

    let navigationController = UINavigationController(rootViewController: tabbar)

    navigationController.pushViewController(viewcontroller, animated: false)

    self.present(navigationController, animated: true, completion: nil)

In this case, the behaviour is so strange... I appear in the correct controller, but with a back button. If I click I go to the same correct controller... with the tabbarcontroller... and then navigationcontroller disappear... nice :S

Any way to go programmatically to the correct controller and get working the tabbar & navigation?

1
have you tried checking "Is Initial View Controller" in the storyboard attributes for the tab bar controller, then simply doing UIStoryboard(..).instantiateInitialViewController()? - wfbarksdale
In all examples you created navigation controller programmatically, try get navigation controller from Inside storyboard by id - svvoff
Where are you to start with? Are you at Tab "A" and you want to load I need to go here as the RootViewController of a NavigationController as Tab "B"? - DonMag
@wbarksdale guided me to the right answer. The key is check 'is initial view controller' and present the tabbarcontroller. - Xavier Garcia Rubio

1 Answers

3
votes

Try out below code:

var storyBoard = UIStoryboard(name: "SecondStoryboard", bundle: nil)
var tabbar: UITabBarController? = (storyBoard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
navigationController?.pushViewController(tabbar, animated: true)

I have less knowledge in swift. Objective-C code for the above is:

    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil];
    UITabBarController *tabbar = (UITabBarController *)[storyBoard instantiateViewControllerWithIdentifier:@"tabbar"];
    [self.navigationController pushViewController:tabbar animated:YES];