简体   繁体   English

使用故事板视图控制器创建程序化选项卡栏?

[英]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. 我可以在没有故事板的标签栏中成功加载视图(请参阅下面的代码),但视图仅部分显示,因为某些UI组件位于故事板中。

The name of my storyboard is MainStoryboard and I set the storyboard view identifier to SettingsViewController. 我的故事板的名称是MainStoryboard,我将故事板视图标识符设置为SettingsViewController。

How can I initialize my storyboard for SettingsViewController in the code below? 如何在下面的代码中初始化SettingsViewController的故事板?

- (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 斯威夫特4

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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