Iphone開發(十三)用導航控制器實現視圖的分層切換(UINavigationController)

holydancer原創,如需轉載,請在顯要位置註明:

轉自holydancer的CSDN專欄,原文地址:http://blog.csdn.net/holydancer/article/details/7484341

 

Iphone和android手機的一個不同的地方是,大部分的android手機都有返回鍵,而Iphone只有一個home鍵,所以我們會發現在Iphone的大部分應用中會在頂部有一個導航條,比如系統的設置界面,該導航條完全按照棧的方式來管理,所以可以很方便的實現後退的操作:

 

今天下了春雨,心情不錯,總結一下導航條的使用;

導航欄這個控件稱爲UINavigationController,常常用來作根視圖控制器,生成對象後可以用該對象push UIViewController的對象,這樣該UIViewController的對象就加到導航條的下部了,可以給視圖控制器加title,會顯示在導航欄上,也可以修改返回鍵的title,如右上圖的左上角的setting按鈕,如果默認的話是沒有viewController的title的,返回鍵的title會是viewController的名字,接下來我們要用Xcode4.3中最基礎的模板EmptyApplication來從頭創建一個最原始的導航demo;

首先,新建一個project,選擇EmptyApplication模板;


這樣生成後是隻有委託類的:


然後new兩個ViewController出來,爲了方便操作記得要附上xib文件;起名爲FirstViewController,SecondViewController,在FirstViewController的xib文件中拖上一個button,連接一個nextClick方法用來切換到下一個視圖;


現在開始操作代碼

AppDelegate.h:

 

[plain]  view plain copy
 
  1. <span style="font-size:18px;">#import <UIKit/UIKit.h>  
  2.   
  3. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  4.   
  5. @property (strong, nonatomic) UIWindow *window;  
  6. @property (strong, nonatomic)  UINavigationController *naviController;  
  7. @end</span>  


AppDelegate.m:

 

 

[plain]  view plain copy
 
  1. <span style="font-size:18px;">#import "AppDelegate.h"  
  2. #import "FirstViewController.h"  
  3. @implementation AppDelegate  
  4.   
  5. @synthesize window = _window;  
  6. @synthesize naviController;  
  7.   
  8. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  9. {  
  10.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  11.       
  12.     //生成一個ViewControlle對象作爲導航欄的第一個視圖;  
  13.     FirstViewController *firstView = [[FirstViewController alloc]init];  
  14.       
  15.     naviController = [[UINavigationController alloc]initWithRootViewController:firstView];  
  16.     //將該導航欄作爲根視圖控制器;  
  17.     self.window.rootViewController = naviController ;  
  18.     [self.window makeKeyAndVisible];  
  19.     return YES;  
  20. }  
  21. </span>  


FirstViewController.h:

 

 

[plain]  view plain copy
 
  1. <span style="font-size:18px;">#import <UIKit/UIKit.h>  
  2.   
  3. @interface FirstViewController : UIViewController  
  4. - (IBAction)nextClick:(id)sender;  
  5.   
  6. @end</span>  


FirstViewController.m:

 

 

[plain]  view plain copy
 
  1. <span style="font-size:18px;">#import "FirstViewController.h"  
  2. #import "SecondViewController.h"  
  3. @interface FirstViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation FirstViewController  
  8.   
  9. - (IBAction)nextClick:(id)sender {  
  10.     SecondViewController *secondView = [[SecondViewController alloc]init];  
  11.     [self.navigationController pushViewController:secondView animated:YES];  
  12. }</span>  


上面只是貼出了需要修改的代碼,其餘自動生成的都不用管,甚至SecondViewController都沒有進行操作,所以我們會看到這樣一種最簡單的導航控制效果;

 

關鍵字:UINavigationController , IOS ,Iphone 開發 ,導航控制器