将UITabBarController与UINavigationController结合使用

我试图使用一个“选项卡式应用程序”与导航栏中。 使用默认的标签栏工作正常,但我不能gat导航栏。 我发现了一些关于推动导航栏的东西和类似的东西,但是我发现的所有东西都是在几年前,所以不要帮助我。 而最近的东西已经过时了,自iOS5和Xcode的新版本..

任何人都可以指出我正确的方向来结合一个解决这个问题?

谨记以下事实:

  • 我正在开发iOS5
  • 我正在使用Xcode 4.2

以下是如何以编程方式实现它。

在[appName] -Info.plist中删除对主要xib的引用

在main.m中,加载你的委托:

int retVal = UIApplicationMain(argc, argv, nil, @"myAppDelegate"); 

在应用程序委托中,在navigationController中加载tabBar,导航控制器和视图。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // create window since nib is not. CGRect windowBounds = [[UIScreen mainScreen] applicationFrame]; windowBounds.origin.y = 0.0; [self setWindow:[[UIWindow alloc] initWithFrame:windowBounds]]; // View Controllers for tabController (one viewController per tab) NSMutableArray *viewControllers = [[NSMutableArray alloc] init]; // first tab has view controller in navigation controller FirstView *firstView = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstView]; [viewControllers addObject:navController]; SecondView *secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil]; [viewControllers addObject:secondView]; // create the tab controller and add the view controllers UITabBarController *tabController = [[UITabBarController alloc] init]; [tabController setViewControllers:viewControllers]; // add tabbar and show [[self window] addSubview:[tabController view]]; [self.window makeKeyAndVisible]; return YES; } 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. NSMutableArray *arrayViewController = [[NSMutableArray alloc] init]; UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil]; UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1]; [arrayViewController addObject:navigationController1]; UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil]; UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2]; [arrayViewController addObject:navigationController2]; self.tabBarController = [[UITabBarController alloc] init]; self.tabBarController.viewControllers = arrayViewController; self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; return YES; } 

在iOS 5中,不再允许更改选项卡的视图控制器(在iOS5之前没有问题)。 唯一可接受的控制器是在IB中为该选项卡定义的控制器。 所以在这个选项卡上安装一个导航控制器是必要的,并给他的观点导航栏。 然后,您可以推送或popup所需的视图,而无需更改选项卡的控制器。

基本的理论是你创build一个UITabBarController,然后把一个UINavigationController放在里面,然后把一个UIViewController作为导航控制器的根视图控制器。 bryanmac刚刚回答了一个很好的代码示例。