UINavigationController和UITabBarController合用
一、创建一个 Tabbed Application.默认创建的是带有两个Tab的工程。
二、在AppDelegate.h里面添加
- @property (strong, nonatomic) UINavigationController *NaviView1Controller;
- @property (strong, nonatomic) UINavigationController *NaviView2Controller;
三、修改AppDelegate.m文件的didFinishLaunchingWithOptions函数,在这里我们设置相应的UINavigationController.
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
- // Override point for customization after application launch.
- UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
- UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
- self.tabBarController = [[[UITabBarController alloc] init] autorelease];
- self.tabBarController.viewControllers = @[viewController1, viewController2];
- self.window.rootViewController = self.tabBarController;
- [self.window makeKeyAndVisible];
- return YES;
- }
修改后:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
- // Override point for customization after application launch.
- UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
- viewController1.title = @"View1";
- self.NaviView1Controller = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
- UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
- viewController2.title = @"View2";
- self.NaviView2Controller = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
- self.tabBarController = [[[UITabBarController alloc] init] autorelease];
- self.tabBarController.viewControllers = @[self.NaviView1Controller, self.NaviView2Controller];
- self.window.rootViewController = self.tabBarController;
- [self.window makeKeyAndVisible];
- return YES;
- }
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
- // Override point for customization after application launch.
- UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
- viewController1.title = @"View1";
- self.NaviView1Controller = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
- UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
- viewController2.title = @"View2";
- self.NaviView2Controller = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
- self.tabBarController = [[[UITabBarController alloc] init] autorelease];
- self.tabBarController.viewControllers = @[self.NaviView1Controller, self.NaviView2Controller];
- self.window.rootViewController = self.tabBarController;
- [self.window makeKeyAndVisible];
- return YES;
- }