實現的功能:通過Navigation,實現多視圖切換。這是使用最多的一種多視圖實現方式。
關鍵詞:多視圖 Navigation UINagivationController
1、創建一個Empty Application工程,命名爲:MultiView-Navigation,如下圖
2、選中工程中的Group MultiView-Tab,然後按住CMD(Windows鍵)+N,新建視圖控制器FirstViewController
3、依照上步操作,新建視圖控制器SecondViewController
4、修改AppDelegate.h,修改後如下:
- //
- // AppDelegate.h
- // MultiView-Navigation
- //
- // Created by Zhang Yanguang on 12-11-21.
- // Copyright (c) 2012年 MyCompanyName. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "FirstViewController.h"
- #import "SecondViewController.h"
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
- @property (strong, nonatomic) UIWindow *window;
- @property (strong, nonatomic) UINavigationController *navigationController;//導航視圖控制器對象
- @end
5、修改AppDelegate.m,主要是修改didFinishLaunchingWithOptions方法,修改後如下:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- FirstViewController *firstViewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
- //初始化navigationController,將firstViewController作爲根視圖控制器
- navigationController = [[UINavigationController alloc]initWithRootViewController:firstViewController];//備註1
- [self.window addSubview:navigationController.view];
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- return YES;
- }
6、修改FirstViewController.h,添加goSecondView方法:
- //
- // FirstViewController.h
- // MultiView-Navigation
- //
- // Created by Zhang Yanguang on 12-11-21.
- // Copyright (c) 2012年 MyCompanyName. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "SecondViewController.h"
- @interface FirstViewController : UIViewController
- -(void)goSecondView;
- @end
7、修改FirstViewController.m,主要是修改initWithNibName及實現goSecondView方法:
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- self.title = @"First View";
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"SecondView" style:UIBarButtonItemStyleBordered target:self action:@selector(goSecondView)];
- }
- return self;
- }
- -(void)goSecondView{
- SecondViewController *secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
- //跳轉到secondViewController
- [self.navigationController pushViewController:secondViewController animated:YES];//備註2
- }
8、視圖控制器SecondViewController,僅修改SecondViewController.m中的initWithNibName方法,如下:
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- self.title = @"SecondView";
- }
- return self;
- }
代碼解釋:
備註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修改後如下:
- //
- // AppDelegate.h
- // TableViewDemo
- //
- // Created by Zhang Yanguang on 12-10-25.
- // 博文:http://blog.csdn.net/m_changgong/article/details/8115137
- // Copyright (c) 2012年 MyCompanyName. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @class ViewController;
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
- @property (strong, nonatomic) UIWindow *window;
- @property (strong, nonatomic) ViewController *viewController;
- //添加navigationController
- @property (strong, nonatomic) UINavigationController *navigationController;
- @end
修改AppDelegate.m中的didFinishLaunchingWithOptions方法,如下:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
- // Override point for customization after application launch.
- self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
- //註釋掉下面一行代碼
- /*
- self.window.rootViewController = self.viewController;*/
- //添加如下代碼
- self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.viewController];
- [self.window addSubview:self.navigationController.view];
- [self.window makeKeyAndVisible];
- return YES;
- }
13、新建視圖控制器AppViewController(帶xib),如下:
14、ViewController.h修改後如下:
- //
- // ViewController.h
- // TableViewDemo
- //
- // Created by Zhang Yanguang on 12-10-25.
- // Copyright (c) 2012年 MyCompanyName. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "AppViewController.h"
- @interface ViewController : UIViewController
- @property(nonatomic,retain)NSMutableArray *apps;
- @property(nonatomic,retain)AppViewController *appViewController;
- @end
ViewController.m中主要是實現了-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法,如下:
- //實現didSelectRowAtIndexPath
- -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- NSString *imageName = [NSString stringWithFormat:@"%d",[indexPath row]+1];
- NSString *appName = [apps objectAtIndex:[indexPath row]];
- //初始化appViewController
- appViewController = [[AppViewController alloc]initWithNibName:@"AppViewController" bundle:nil];
- //傳遞參數
- appViewController.appName = appName;
- appViewController.appIconName = imageName;
- //跳轉到appViewController
- [self.navigationController pushViewController:appViewController animated:YES];
- }
15、AppViewController.h如下:
- //
- // AppViewController.h
- // TableViewDemo
- //
- // Created by Zhang Yanguang on 12-11-21.
- // Copyright (c) 2012年 MyCompanyName. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface AppViewController : UIViewController
- @property(strong,nonatomic)NSString *appName;
- @property(strong,nonatomic)NSString *appIconName;
- @property(strong,nonatomic)IBOutlet UILabel *appNameLabel;
- @property(strong,nonatomic)IBOutlet UIImageView *appIconImgView;
- @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;">
- </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
- //
- // AppViewController.m
- // TableViewDemo
- //
- // Created by Zhang Yanguang on 12-11-21.
- // Copyright (c) 2012年 MyCompanyName. All rights reserved.
- //
- #import "AppViewController.h"
- @interface AppViewController ()
- @end
- @implementation AppViewController
- @synthesize appName;
- @synthesize appIconName;
- @synthesize appNameLabel;
- @synthesize appIconImgView;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- self.title = @"AppViewController";
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- self.appNameLabel.text = appName;
- self.appIconImgView.image = [UIImage imageNamed:appIconName];
- NSLog(@"appName=%@,appIconName=%@",appName,appIconName);
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- appName = nil;
- appIconName = nil;
- appNameLabel = nil;
- appIconImgView = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- @end