如何在所有视图中显示选项卡栏

时间:2014-08-01 06:14:02

标签: ios

以下是我的代码,它仅在给定的2个标签栏项目上显示标签栏,但在其他视图中没有显示标签栏。

 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.startQuizViewController = [[StartQuizViewController alloc] initWithNibName:nil bundle:nil];
self.scoreViewController = [[ScoreViewController alloc] initWithNibName:nil bundle:nil];

self.startQuizViewController.title = @"QUIZ";
self.scoreViewController.title = @"SCORES";

self.tabBarController = [[UITabBarController alloc] init];

self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.startQuizViewController,self.scoreViewController,nil];

_navigationController=[[UINavigationController alloc]initWithRootViewController:self.tabBarController];

[self.window addSubview:self.navigationController.view];
self.window.rootViewController = self.navigationController;

[self.window makeKeyAndVisible];
return YES;

didFinishLaunchingWithOptions

AppDelegate.m.编写的代码

2 个答案:

答案 0 :(得分:1)

更改此部分:

_navigationController=[[UINavigationController alloc]initWithRootViewController:self.tabBarController];

[self.window addSubview:self.navigationController.view];
self.window.rootViewController = self.navigationController;

到此:

self.window.rootViewController = self.tabBarController;

你不能在导航控制器中安装tabbarcontroller。所以只需删除导航控制器并将标签栏控制器设置为根视图控制器

编辑:

self.startQuizViewController = [[StartQuizViewController alloc] initWithNibName:nil bundle:nil];
self.scoreViewController = [[ScoreViewController alloc] initWithNibName:nil bundle:nil];

self.startQuizViewController.title = @"QUIZ";
self.scoreViewController.title = @"SCORES";

self.tabBarController = [[UITabBarController alloc] init];

UINavigationController * nav1 = [[UINavigationController alloc] initWithRootViewController:self.startQuizViewController];

UINavigationController * nav2 = [[UINavigationController alloc] initWithRootViewController:self.scoreViewController];


self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nil];


self.window.rootViewController = self.tabBarController;

[self.window makeKeyAndVisible];

是的。你现在可以在这些控制器内推动视图控制器,并且可以随时使用标签栏。

答案 1 :(得分:0)

使用此

UIViewController  *viewController1, *viewController2;

    viewController1 = [[UIViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] ;
    UINavigationController *navigationcontroller = [[UINavigationController alloc] initWithRootViewController:viewController1] ;

    viewController2 = [[UIViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] ;
 UINavigationController *navigationcontroller2 = [[UINavigationController alloc] initWithRootViewController:viewController2] ;


    self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller, navigationcontroller2, nil];

最后

self.window.rootViewController = self.tabBarController;

通过这种方式,您可以为两个标签栏控制器提供单独的导航控制器。