導航欄(UINavigationBar)是 iOS App 中最常見的頁面骨架之一,幾乎每個多層級的應用都離不開它。本文以一個「root → First → Second → Third」的導航順序為例,示範如何建立導航控制器、設定常用屬性,以及頁面之間的值傳遞。
檔案目錄如下:基本導航順序為 root → First → Second → Third,其中 FirstViewController 作為 navigation 堆疊的 root view。

如果想直接把 navigation 作為專案一開始的根視圖,可把 RootViewController.h 中的 nav 屬性移到 AppDelegate.h,再把 action 中的程式碼複製到 AppDelegate.m 的 didFinishLaunchingWithOptions 方法裡,最後將 self.window.rootViewController 設為 UINavigationController 類型的 nav 即可。
在 RootViewController.h 中宣告:
#import <UIKit/UIKit.h>
@class FirstViewController;
@interface RootViewController : UIViewController
@property (strong, nonatomic) UINavigationController *nav;
- (IBAction)btnClick:(UIButton *)sender;
@end
在 RootViewController.m 的 action 中建立導航控制器並跳轉:
- (IBAction)btnClick:(UIButton *)sender {
FirstViewController *fristview = [[[FirstViewController alloc] init] autorelease];
//初始化 UINavigationController(方式一)
self.nav = [[[UINavigationController alloc] initWithRootViewController:fristview] autorelease];
//跳轉到 FirstView 頁面
[self presentViewController:self.nav animated:YES completion:nil];
}
導航建立好之後,就可以在 viewDidLoad 中設定各項屬性:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setTranslucent:NO]; //設置 navigationbar 的半透明
self.title = @"navigationcontroller"; //設置 navigationbar 上顯示的標題
[self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]]; //設置 navigationbar 的顏色
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:nil]; //左邊按鈕
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:nil]; //右邊按鈕
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]]; //設置左右按鈕字體顏色
}
效果圖如下:

還有一個常用屬性——titleView,可以把標題替換成自訂控制項,例如 UISegmentedControl:
NSArray *arr = [NSArray arrayWithObjects:@"1",@"2", nil];
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:arr];
self.navigationItem.titleView = segment; //設置 navigation 上的 titleview
效果如下,可以看到中間的標題變成了兩個可選的按鈕:

接著示範兩個視圖之間如何傳遞資料。在第二個視圖中宣告一個成員變數來接收從第一個視圖傳來的值:
@interface SecondViewController : UIViewController
@property (copy,nonatomic) NSString *str;
@end
在 SecondViewController 的 viewDidLoad 中用一個 button 顯示接收到的值:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"second";
UIButton *aBUTTON = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 50, 30)];
[aBUTTON setTitle:_str forState:UIControlStateNormal];
[aBUTTON addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aBUTTON];
}
在 RootViewController 為右邊按鈕加上事件,點擊後 push 第二個視圖並傳值:
- (void)clicked{
SecondViewController *second = [[SecondViewController alloc]init];
[self.navigationController pushViewController:second animated:YES];
second.str = @"hello!!";
[second release];
}
執行後點擊按鈕,第二個視圖會被推出,button 上顯示傳過來的值:

若要返回上一頁,則使用 popViewControllerAnimated:
- (void)clicked{
[self.navigationController popViewControllerAnimated:YES];
}
這樣就能回到第一個視圖。透過 push 與 pop 的搭配,再加上屬性傳值,就能輕鬆完成多層級的頁面導航與資料流轉。