Search code examples
iosuitabbarcontrollerpushviewcontrolleruitabview

Tabs do not show when using PushViewController


I'm trying to use the following code to display a tabbarcontroller

UITabBarController *tc = [[self storyboard] instantiateViewControllerWithIdentifier:@"tabbarcontroller"];
[self.navigationController pushViewController:tc animated:YES];

It does load the view, and I can tell it which of the tabs I want it to default to. The problem is the tabs don't show. From what I've read I gather it has something to do with putting the tab controller inside of the navigation controller, but I couldn't find any solutions.


Solution

  • If you use Storyboard, use pushViewController method is a bad choice (also if it work). You have to insert a "segue". Go in the storyboard and while press ctrl button, click on the main controller (which must open the tabViewController) and then release the click on the tabBarController.

    Now you have the segue. Click on the circle which appears and choose an identifier for this segue, for example: MainToTab .

    enter image description here

    Now in your method, you have just to call:

    [self performSegueWithIdentifier:@"MainToTab" sender:self];
    

    Moreover, if you want manage the property on the destination controller (by segue), you can implement this method:

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
        if ([segue.identifier isEqualToString:@"MainToTab"]) {
            UITabViewController *tb = (UITabViewController *)segue.destinationViewController;
            //set the properties...
        }
    
    }
    

    This method is called automatically when you launch the previous method.