简体   繁体   中英

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]]; 
}

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"];

Swift 4

let storyboard = UIStoryboard(name: "Main", bundle: nil)
settingsViewController = storyboard.instantiateInitialViewController()
settingsViewController = storyboard.instantiateViewController(withIdentifier: "SettingsViewController")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM