Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a programmatic tab bar with storyboard view controllers?

I have a tab bar that is created programmatically and I'm having difficulties initializing a storyboard associated with a view.

I'm able to load the view successfully in the tab bar without the storyboard (see code below) but the view is only partially showing because some of the UI components are in the storyboard.

The name of my storyboard is MainStoryboard and I set the storyboard view identifier to SettingsViewController.

How can I initialize my storyboard for SettingsViewController in the code below?

- (void)createTabBarItems {
    tabBarController = [[UITabBarController alloc] init];

    settingsViewController  = [[SettingsViewController alloc] init];


    UINavigationController *sett = [[[UINavigationController alloc]
                                     initWithRootViewController: settingsViewController] autorelease];

    [sett.tabBarItem setTitle:@"Settings"];
    [sett.tabBarItem setImage:[UIImage imageNamed:@"settings.png"]];

    [tabBarController setViewControllers:
        [NSArray arrayWithObjects:sett, sett, sett, sett, nil]]; 
}
like image 811
kevin Avatar asked Nov 13 '11 17:11

kevin


People also ask

How do I add a controller to my tab bar controller storyboard?

Open Main. storyboard and select the map view controller. From Xcode's Editor menu, choose Embed In → Tab Bar Controller. This will add the map view controller to the view controllers array of a new tab bar controller.

How do I add a tab bar to a storyboard?

Add a new Tab Bar ItemDrag View controller into the storyboard. Drag Tab Bar Item into the newly created view controller, it will sit at the same level of the View .

What is a tab bar controller?

A tab bar controller is a powerful UI component for iOS apps. It's a container view, and you use it to group view controllers together. They give your app's user access to the most important screens of your app.


1 Answers

If you want to initialize the view controller as in the storyboard you have to use the storyboard methods instead of allocating the view controller directly:

// load the storyboard by name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

// either one of the two, depending on if your view controller is the initial one
settingsViewController = [storyboard instantiateInitialViewController];
settingsViewController = [storyboard instantiateViewControllerWithIdentifier:@"SettingsViewController"];
like image 145
Dennis Bliefernicht Avatar answered Sep 19 '22 15:09

Dennis Bliefernicht