我尝试将导航控制器添加到标签栏控制器,但标签显示为黑色。
我的代码: AppDelegate.m
#import "SettingsNavigationControllerViewController.h"
#import "SettingsViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2, *viewController3;
SettingsNavigationControllerViewController *viewController4;
UINavigationController *navigationController = [UINavigationController alloc];
viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
viewController3 = [[ShareViewController alloc] initWithNibName:@"ShareViewController" bundle:nil];
SettingsViewController *settingViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
viewController4 = [[SettingsNavigationControllerViewController alloc] initWithRootViewController:settingViewController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, navigationController];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
提前谢谢
答案 0 :(得分:0)
这是因为你添加了一个没有 RootViewController 的导航控制器,甚至没有初始化。这就是为什么视图显示为黑色。可能您想添加 viewController4 而不是 navigationController 。所以改变你的代码如下
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4];
答案 1 :(得分:0)
您需要先使用rootviewcontrollers创建导航控制器
viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
然后像这样实例化Tab Bar控制器
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, nil];
答案 2 :(得分:0)
首先创建导航控制器。
UINavigationController *nav1 = [[UINavigationController alloc] init];
UIViewController *viewController1 = [[[FirstSteps alloc] initWithNibName:@"FirstView" bundle:nil] autorelease];
nav1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
UINavigationController *nav2 = [[UINavigationController alloc] init];
UIViewController *viewController2 = [[[Profiles alloc] initWithNibName:@"SecondView" bundle:nil] autorelease];
nav2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
然后像这样初始化你的标签栏控制器。
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2 ,nil];
self.window.rootViewController=self.tabBarController;
希望这会对你有所帮助。
答案 3 :(得分:0)
嗨伙伴请访问以下链接以获取标签栏示例。
我认为这是一个很好的例子,对你有所帮助。
https://github.com/alikaragoz/AKTabBarController
您只需在appDelegate.m文件中设置控制器名称