本文示範如何建立 UINavigationController,並逐一調整它的常用屬性。示範的頁面順序為 root → First → Second → Third,其中 FirstViewController 位於導航堆棧的最底層,也就是 root view。

若希望 App 一啟動就直接進入導航結構,最直白的做法是把 nav 屬性從 RootViewController.h 搬到 AppDelegate.h,再把原本的 action 程式碼搬到 didFinishLaunchingWithOptions 裡,最後將 self.window.rootViewController 設為這個 UINavigationController。
RootViewController.h:
#import <UIKit/UIKit.h> @class FirstViewController; @interface RootViewController : UIViewController @property (strong, nonatomic) UINavigationController *nav; - (IBAction)btnClick:(UIButton *)sender; @end
RootViewController.m 的按鈕事件裡:
- (IBAction)btnClick:(UIButton *)sender {
// 建立要顯示的 viewController
FirstViewController *fristview = [[FirstViewController alloc] init];
// 方式一:直接以 rootViewController 初始化
self.nav = [[UINavigationController alloc] initWithRootViewController:fristview];
// 方式二:先初始化再 push
// self.nav = [[UINavigationController alloc] init];
// [self.nav pushViewController:fristview animated:YES];
// 方式三:以 xib 名稱初始化(示範環境下載入失敗,原因待查)
// self.nav = [[UINavigationController alloc] initWithNibName:@"FirstViewController" bundle:nil];
// 以模態方式呈現
[self presentViewController:self.nav animated:YES completion:nil];
// 注意:addSubview 這種寫法適合往 view 裡塞 button、label、textField 等小元件,
// 並不適合拿來做整頁跳轉
// [self.view addSubview:self.nav.view];
}
導航控制器掛上之後,接著調整外觀。以下都在 viewDidLoad 中完成:
- (void)viewDidLoad {
[super viewDidLoad];
// 是否半透明
[self.navigationController.navigationBar setTranslucent:NO];
// 標題文字
self.title = @"navigationcontroller";
// 導航列底色
[self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];
// 左右兩側按鈕
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]];
}

還有一個很實用的屬性 navigationItem.titleView,可以把標題位置換成自訂元件,例如分頁切換用的 UISegmentedControl:
NSArray *arr = [NSArray arrayWithObjects:@"1", @"2", nil]; UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:arr]; self.navigationItem.titleView = segment;

執行後可以看到原本的文字標題變成了一組可切換的按鈕,這就是 titleView 的效果。
接著看兩個視圖如何溝通。第二個視圖先準備一個成員變數用來接收值:
@interface SecondViewController : UIViewController @property (copy, nonatomic) NSString *str; @end
並用一個按鈕把收到的值顯示出來:
- (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];
}
回到第一個視圖,為右側按鈕加上跳轉事件,順便把值帶過去:
- (void)clicked {
SecondViewController *second = [[SecondViewController alloc] init];
[self.navigationController pushViewController:second animated:YES];
second.str = @"hello!!";
}

執行後點擊按鈕,第二個視圖被推入堆棧,按鈕上顯示的正是剛剛傳過去的值。
返回上一頁則使用 pop:
- (void)clicked {
[self.navigationController popViewControllerAnimated:YES];
}
補充一個實務上的順序問題:pushViewController:animated: 之後才設定屬性其實是安全的,因為 push 只是把控制器壓入堆棧,畫面要到下一個 run loop 才真正渲染,所以屬性仍來得及生效。不過若要在跳轉前根據當前狀態決定是否放行,就應該把判斷寫在 push 之前。