iPhone开发【十】多视图技术总结之三:Navigation

来源:互联网 发布:知乎 高中 书包 编辑:程序博客网 时间:2024/04/28 15:19

转载请注明出处,原文网址:http://blog.csdn.net/m_changgong/article/details/8209309 作者:张燕广

实现的功能:通过Navigation,实现多视图切换。这是使用最多的一种多视图实现方式。

关键词:多视图 Navigation UINagivationController

1、创建一个Empty Application工程,命名为:MultiView-Navigation,如下图


2、选中工程中的Group MultiView-Tab,然后按住CMD(Windows键)+N,新建视图控制器FirstViewController,如下图


3、依照上步操作,新建视图控制器SecondViewController

4、修改AppDelegate.h,修改后如下:

[cpp] view plaincopy
  1. //  
  2. //  AppDelegate.h  
  3. //  MultiView-Navigation  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-21.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import "FirstViewController.h"  
  11. #import "SecondViewController.h"  
  12.   
  13. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  14.   
  15. @property (strong, nonatomic) UIWindow *window;  
  16.   
  17. @property (strong, nonatomic) UINavigationController *navigationController;//导航视图控制器对象  
  18. @end  
5、修改AppDelegate.m,主要是修改didFinishLaunchingWithOptions方法,修改后如下:

[cpp] view plaincopy
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  4.     // Override point for customization after application launch.  
  5.     FirstViewController *firstViewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];  
  6.     //初始化navigationController,将firstViewController作为根视图控制器  
  7.     navigationController = [[UINavigationController alloc]initWithRootViewController:firstViewController];//备注1  
  8.     [self.window addSubview:navigationController.view];  
  9.       
  10.     self.window.backgroundColor = [UIColor whiteColor];  
  11.     [self.window makeKeyAndVisible];  
  12.     return YES;  
  13. }  

6修改FirstViewController.h,添加goSecondView方法:

[cpp] view plaincopy
  1. //  
  2. //  FirstViewController.h  
  3. //  MultiView-Navigation  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-21.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import "SecondViewController.h"  
  11. @interface FirstViewController : UIViewController  
  12.   
  13. -(void)goSecondView;  
  14. @end  
7修改FirstViewController.m主要是修改initWithNibName及实现goSecondView方法:

[cpp] view plaincopy
  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  2. {  
  3.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  4.     if (self) {  
  5.         // Custom initialization  
  6.         self.title = @"First View";  
  7.         self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"SecondView" style:UIBarButtonItemStyleBordered target:self action:@selector(goSecondView)];  
  8.           
  9.     }  
  10.     return self;  
  11. }  
  12.   
  13. -(void)goSecondView{  
  14.     SecondViewController *secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];  
  15.     //跳转到secondViewController  
  16.     [self.navigationController pushViewController:secondViewController animated:YES];//备注2  
  17. }  

8、视图控制器SecondViewController,仅修改SecondViewController.m中的initWithNibName方法,如下:

[cpp] view plaincopy
  1. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  2. {  
  3.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  4.     if (self) {  
  5.         // Custom initialization  
  6.         self.title = @"SecondView";  
  7.     }  
  8.     return self;  
  9. }  
代码解释:

备注1:firstViewController被push到navigationController中。

备注2:self.navigationController的英文注释为:// If this view controller has been pushed onto a navigation controller, return it.

所以self.navigationController返回的正是备注1中的navigationController

9、编译、运行,效果如下:


10、Navigation通常与TableView搭配使用,博文iPhone开发【七】常用控件之表TableView 编写了一个TableView的示例,那是一个单视图应用,现在搭配上Navigation将其修改为多视图应用。

11、将TableView示例工程复制一份名称修改为TableViewDemo-Nav,打开该工程进行修改。

12、首先,修改AppDelegate,添加UINavigationController的实例,AppDelegate.h修改后如下:

[cpp] view plaincopy
  1. //  
  2. //  AppDelegate.h  
  3. //  TableViewDemo  
  4. //  
  5. //  Created by Zhang Yanguang on 12-10-25.  
  6. //  博文:http://blog.csdn.net/m_changgong/article/details/8115137  
  7. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  8. //  
  9.   
  10. #import <UIKit/UIKit.h>  
  11.   
  12. @class ViewController;  
  13.   
  14. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  15.   
  16. @property (strong, nonatomic) UIWindow *window;  
  17.   
  18. @property (strong, nonatomic) ViewController *viewController;  
  19. //添加navigationController  
  20. @property (strong, nonatomic) UINavigationController *navigationController;  
  21. @end  

修改AppDelegate.m中的didFinishLaunchingWithOptions方法,如下:

[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.     self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];  
  6.     //注释掉下面一行代码  
  7.     /* 
  8.     self.window.rootViewController = self.viewController;*/  
  9.     //添加如下代码  
  10.     self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.viewController];  
  11.     [self.window addSubview:self.navigationController.view];  
  12.       
  13.     [self.window makeKeyAndVisible];  
  14.     return YES;  
  15. }  
13、新建视图控制器AppViewController(带xib),如下:


14、ViewController.h修改后如下:

[cpp] view plaincopy
  1. //  
  2. //  ViewController.h  
  3. //  TableViewDemo  
  4. //  
  5. //  Created by Zhang Yanguang on 12-10-25.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import "AppViewController.h"  
  11. @interface ViewController : UIViewController  
  12.   
  13. @property(nonatomic,retain)NSMutableArray *apps;  
  14. @property(nonatomic,retain)AppViewController *appViewController;  
  15. @end  
ViewController.m中主要是实现了-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法,如下:

[cpp] view plaincopy
  1. //实现didSelectRowAtIndexPath  
  2. -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  3.     NSString *imageName = [NSString stringWithFormat:@"%d",[indexPath row]+1];  
  4.     NSString *appName = [apps objectAtIndex:[indexPath row]];  
  5.     //初始化appViewController  
  6.     appViewController = [[AppViewController alloc]initWithNibName:@"AppViewController" bundle:nil];  
  7.     //传递参数  
  8.     appViewController.appName = appName;  
  9.     appViewController.appIconName = imageName;  
  10.     //跳转到appViewController  
  11.     [self.navigationController pushViewController:appViewController animated:YES];  
  12. }  
15、AppViewController.h如下:
[cpp] view plaincopy
  1. //  
  2. //  AppViewController.h  
  3. //  TableViewDemo  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-21.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface AppViewController : UIViewController  
  12.   
  13. @property(strong,nonatomic)NSString *appName;  
  14. @property(strong,nonatomic)NSString *appIconName;  
  15.   
  16. @property(strong,nonatomic)IBOutlet UILabel *appNameLabel;  
  17. @property(strong,nonatomic)IBOutlet UIImageView *appIconImgView;  
  18. @end <font face="Microsoft YaHei" size="4"><font size="4"><font size="4"><font size="4"><font size="4"><font size="4"><font size="4"><font size="4"><font size="4"><font size="4"><font size="4"><font size="4"><font face="Microsoft YaHei" size="4"><span style="font-family:Microsoft YaHei;font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">  
  19. </span></span></span></span></span></span></span></span></span></font></font></font></font></font></font></font></font></font></font></font></font></font>  

注意:将输出口与AppViewController.xib中的UI控件相连。AppViewController.xib如下:


[cpp] view plaincopy
  1. //  
  2. //  AppViewController.m  
  3. //  TableViewDemo  
  4. //  
  5. //  Created by Zhang Yanguang on 12-11-21.  
  6. //  Copyright (c) 2012年 MyCompanyName. All rights reserved.  
  7. //  
  8.   
  9. #import "AppViewController.h"  
  10.   
  11. @interface AppViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation AppViewController  
  16. @synthesize appName;  
  17. @synthesize appIconName;  
  18. @synthesize appNameLabel;  
  19. @synthesize appIconImgView;  
  20.   
  21. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  22. {  
  23.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  24.     if (self) {  
  25.         self.title = @"AppViewController";  
  26.     }  
  27.     return self;  
  28. }  
  29.   
  30. - (void)viewDidLoad  
  31. {  
  32.     [super viewDidLoad];  
  33.     // Do any additional setup after loading the view from its nib.  
  34.       
  35.     self.appNameLabel.text = appName;  
  36.     self.appIconImgView.image = [UIImage imageNamed:appIconName];  
  37.       
  38.     NSLog(@"appName=%@,appIconName=%@",appName,appIconName);  
  39. }  
  40.   
  41. - (void)viewDidUnload  
  42. {  
  43.     [super viewDidUnload];  
  44.     // Release any retained subviews of the main view.  
  45.     // e.g. self.myOutlet = nil;  
  46.     appName = nil;  
  47.     appIconName = nil;  
  48.     appNameLabel = nil;  
  49.     appIconImgView = nil;  
  50. }  
  51.   
  52. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  53. {  
  54.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  55. }  
  56.   
  57. @end  
16、运行效果如下:


点击下载本文源代码

原创粉丝点击