gpt4 book ai didi

iphone - 以编程方式添加 TabBarController

转载 作者:可可西里 更新时间:2023-11-01 03:38:55 24 4
gpt4 key购买 nike

我想以编程方式制作标签栏 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com