iphone – 以编程方式添加TabBarController
发布时间:2020-12-14 19:51:08 所属栏目:百科 来源:网络整理
导读:我想以编程方式制作标签栏控制器和导航控制器.我的代码到目前为止工作,它在底部显示一个标签栏,但OptionViewController在第二个标签栏的按钮上没有说任何内容(没有标题).有趣的是,当我点击按钮时没有任何东西,标题出现(他的观点也是如此),有人可以向我解释我
我想以编程方式制作标签栏控制器和导航控制器.我的代码到目前为止工作,它在底部显示一个标签栏,但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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |