Question

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.

Was it helpful?

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.

OTHER TIPS

What you want inside your UITabBarController are UIViewControllers, possibly inside a UINavigationController.

You shouldn't as well push a UITabBarController. You can see Apple's explanation here:

An app that uses a tab bar controller can also use navigation controllers in one or more tabs. When combining these two types of view controller in the same user interface, the tab bar controller always acts as the wrapper for the navigation controllers.

The most common way to use a tab bar controller is to embed its view in your app’s main window. (...)

Still you can present it modally:

It is possible (although uncommon) to present a tab bar controller modally in your app. Tab bar interfaces are normally installed in your app’s main window and updated only as needed. However, you could present a tab bar controller modally if the design of your interface seems to warrant it. For example, to toggle from your app’s primary operational mode to a completely different mode that uses a tab bar interface, you could present the secondary tab bar controller modally using a crossfade transition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top