UITabBarController与UINavigationController配合使用

在ios应用开发中,最常用的一种方式就是UITabBarController与UINavigationController配合使用。
这篇文章主要以一个含两个标签面的应用来介绍一下两种情况:
1.UITabBarController各子界面是独立的导航关系,互不影响,启动代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.

// 整体是一个UITabBarController,每个界面都是一个UINavigationCongtroller。
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@”FirstViewController” bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@”SecondViewController” bundle:nil] autorelease];
UINavigationController* nav1 = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
UINavigationController* nav2 = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
UITabBarController* tabBarController = [[[UITabBarController alloc] init] autorelease];
tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2, nil];
// 这里是不同之处是,根界面UITabBarController
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

2.应用整体是一个导航关系,只在根界面上分tab页。启动代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.

// 整体是一个UINavigationController,根界面包含两个tabBar界面。
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@”FirstViewController” bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@”SecondViewController” bundle:nil] autorelease];
UITabBarController* tabBarController = [[[UITabBarController alloc] init] autorelease];
tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1,viewController2, nil];
UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:tabBarController] autorelease];
// 这里是不同之处是,根界面UINavigationController
self.window.rootViewController = navController;

[self.window makeKeyAndVisible];
return YES;
}

你可能感兴趣的:(ios)