多視圖切換

實現的功能:通過Navigation,實現多視圖切換。這是使用最多的一種多視圖實現方式。


關鍵詞:多視圖 Navigation UINagivationController

1、創建一個Empty Application工程,命名爲:MultiView-Navigation,如下圖


2、選中工程中的Group MultiView-Tab,然後按住CMD(Windows鍵)+N,新建視圖控制器FirstViewController

3、依照上步操作,新建視圖控制器SecondViewController

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



  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方法,修改後如下:



  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方法:



  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方法:



  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方法,如下:





  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


10、Navigation通常與TableView搭配使用,博文iPhone開發【七】常用控件之表TableView 編寫了一個TableView的示例,那是一個單視圖應用,現在搭配上Navigation將其修改爲多視圖應用。

11、將TableView示例工程複製一份名稱修改爲TableViewDemo-Nav,打開該工程進行修改。

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


  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方法,如下:



  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修改後如下:


  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方法,如下:





  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如下:


  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  


發佈了92 篇原創文章 · 獲贊 1 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章