UINavigationController和UITabBarController合用[转载比较好理解]

来源:互联网 发布:qq视频变声软件 编辑:程序博客网 时间:2024/03/29 08:18

原文地址:http://blog.csdn.net/lovefqing/article/details/8255846


很多时候我们创建一个基于UITabBarController的application以后还希望能够在每个tab view都可以实现导航控制,即添加一个UINavigationController来实现tabview内部的view之间的切换,这即是本文所要介绍的。


一、创建一个 Tabbed Application.默认创建的是带有两个Tab的工程。

二、在AppDelegate.h里面添加

[cpp] view plaincopy
  1. @property (strong, nonatomic) UINavigationController *NaviView1Controller;  
  2. @property (strong, nonatomic) UINavigationController *NaviView2Controller;  

三、修改AppDelegate.m文件的didFinishLaunchingWithOptions函数,在这里我们设置相应的UINavigationController.

修改前:

[cpp] view plaincopy
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  4.     // Override point for customization after application launch.  
  5.     UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];  
  6.     UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];  
  7.     self.tabBarController = [[[UITabBarController alloc] init] autorelease];  
  8.     self.tabBarController.viewControllers = @[viewController1, viewController2];  
  9.     self.window.rootViewController = self.tabBarController;  
  10.     [self.window makeKeyAndVisible];  
  11.     return YES;  
  12. }  
修改后:

[cpp] view plaincopy
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];  
  4.     // Override point for customization after application launch.  
  5.     UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];  
  6.     viewController1.title = @"View1";  
  7.     self.NaviView1Controller = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];  
  8.   
  9.     UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];  
  10.     viewController2.title = @"View2";  
  11.     self.NaviView2Controller = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];  
  12.   
  13.     self.tabBarController = [[[UITabBarController alloc] init] autorelease];  
  14.       
  15.     self.tabBarController.viewControllers = @[self.NaviView1Controller, self.NaviView2Controller];  
  16.       
  17.     self.window.rootViewController = self.tabBarController;  
  18.     [self.window makeKeyAndVisible];  
  19.     return YES;  
  20. }  

运行一下程序,可以看到每个Tab view都自动多了一个NavigationBar在顶部。


三、添加两个新的View测试NavigationController。

1. 创建Test1ViewController类,以及对应的xib文件。

2. 创建Test2ViewController类,以及对应的xib文件。

3. 如果你的iPhone iOS版本低于6.0,在xib的设置里需要勾掉autolayout选项。


四、在Tab view 1的xib文件里添加一个按钮,并设置相应的IBOutlet, IBAction.如下图:



五、编写代码逻辑。

在FirstViewController.m中。编写函数gotoTest1View

[cpp] view plaincopy
  1. - (IBAction)gotoTest1View:(id)sender {  
  2.     Test1ViewController *SRDVController = [[Test1ViewController alloc]init];  
  3.     [self.navigationController pushViewController:SRDVController animated:YES];  
  4.     [SRDVController release];  
  5. }  
同理可以测试,Test2ViewController.不再赘述。


六、代码下载:http://download.csdn.net/detail/lovefqing/4845092


若本文和代码有任何错误,欢迎拍砖指正!


0 0