iphone - 以编程方式添加 TabBarController

标签 iphone objective-c ios xcode tabbarcontroller

我想以编程方式制作标签栏 Controller 和导航 Controller 。到目前为止,我的代码工作正常,它在底部显示了一个选项卡栏,但 OptionViewController 在第二个选项卡栏的按钮上什么也没说(没有标题)。有趣的是,当我点击没有任何内容的按钮时,标题出现(他的观点也是如此),有人可以向我解释我做错了什么吗?我尝试使用以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];

    DefaultViewController *dvc = [[DefaultViewController alloc] init];
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
    [tabItems addObject:dvc_nc];
    [dvc release];
    [dvc_nc release];

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
    [tabItems addObject:ovc_nc];
    [ovc release];
    [ovc_nc release];

    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = tabItems;
    self.tabController = tbc;
    [tabItems release];
    [tbc release];

    [self.window addSubview:self.tabController.view];

    return YES;
}

最佳答案

您需要设置 UINavigationController 的 tabBarItem 和标题,而不是它的根 viewController。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];

    DefaultViewController *dvc = [[DefaultViewController alloc] init];
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
    dvc_nc.tabBarItem.title = @"Default";
    dvc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]];
    [tabItems addObject:dvc_nc];
    [dvc release];
    [dvc_nc release];

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
    ovc_nc.tabBarItem.title = @"Option"
    ovc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Optiomn" ofType:@"png"]];

    [tabItems addObject:ovc_nc];
    [ovc release];
    [ovc_nc release];

    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = tabItems;
    self.tabController = tbc;
    [tabItems release];
    [tbc release];

    [self.window addSubview:self.tabController.view];

    return YES;
}

关于iphone - 以编程方式添加 TabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7319447/

相关文章:

objective-c - 使用 Shift 键扩展 NSCollectionView 中的选择

ios - 如何在 Swift 中使用 AVPlayer 跟踪歌曲何时播放完毕

c++ - 如何在 iPhone 上使用 LAME 编码为 .mp3 时显示进度条?

objective-c - 需要有关使用 GCD 将 block 添加到队列(objective-c)的建议

iphone - AVAudioSession sharedInstance requestRecordPermission :^(BOOL granted)

iOS 你确定吗对话框

ios - 错误的工具栏位置 - iOS

ios - 在 Today 扩展中添加 UITableView

ios - 如何在 Xcode 中查看 iTunes Connect 的崩溃?

iphone - 在 Xcode 中组织不同版本的应用程序