導航控制項是 iOS 最基礎的畫面堆疊管理工具。本篇整理從建立堆疊、動態推入與移出,到客製導航列內容的完整流程。
在 AppDelegate 的標頭檔宣告屬性:
@property(nonatomic, strong) UINavigationController *nav;
實作檔中建立並掛上主視窗:
@synthesize nav;
-(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.nav = [[UINavigationController alloc] initWithRootViewController:self.rootViewController];
[self.window addSubview:self.nav.view];
return YES;
}
各頁的 viewDidLoad 中設定標題即可:
-(void)viewDidLoad {
self.title = @"hello World";
}
假設要讓第一個畫面出現 5 秒後自動推入第二個:
#import "SecondViewController"
-(void)pushSecondController {
SecondViewController *secondController =
[[SecondViewController alloc] initWithNibName:nil bundle:NULL];
[self.nav pushViewController:secondController animated:YES];
}
-(void)viewDidAppear:(BOOL)paramAnimated {
[super viewDidAppear:paramAnimated];
[self performSelector:@selector(pushSecondController) withObject:nil afterDelay:5.0f];
}
反向操作就是 pop:
-(void)goBack {
[self.nav popViewControllerAnimated:YES];
}
-(void)viewDidAppear:(BOOL)paramAnimated {
[super viewDidAppear:paramAnimated];
[self performSelector:@selector(goBack) withObject:nil afterDelay:5.0f];
}
透過 viewControllers 屬性可以取得並改寫整個堆疊:
-(void)goBack {
NSArray *currentControllers = self.nav.viewControllers;
NSMutableArray *newControllers = [NSMutableArray arrayWithArray:currentControllers];
[newControllers removeLastObject];
self.nav.viewControllers = newControllers;
// 需要動畫時:
// [self.nav setViewControllers:newControllers animated:YES];
}
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIImageView *imageView =
[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 40.0f)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *image = [UIImage imageNamed:@"FullSizeLogo.png"];
[imageView setImage:image];
self.navigationItem.titleView = imageView;
}
navigationItem 提供 leftBarButtonItem 與 rightBarButtonItem 兩個屬性,都接受 UIBarButtonItem:
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"right"
style:UIBarButtonItemStylePlain
target:self
action:@selector(performRight:)];
-(void)performRight:(id)right {
NSLog(@"clicked rightButton");
}
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(performRight:)];
可用的系統項目包含 Done、Cancel、Edit、Save、Add、Compose、Reply、Action、Organize、Bookmarks、Search、Refresh、Stop、Camera、Trash、Play、Pause、Rewind、FastForward、Undo、Redo 等,以及 FlexibleSpace、FixedSpace 這兩個彈性間隔。
UISwitch *mySwitch = [[UISwitch alloc] init];
mySwitch.on = YES;
[mySwitch addTarget:self action:@selector(SwitchChanged:)
forControlEvents:UIControlEventValueChanged];
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithCustomView:mySwitch];
NSArray *items = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"UpArrow.png"],
[UIImage imageNamed:@"DownArrow.png"], nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
[segmentedControl addTarget:self action:@selector(segmentedControlTapped:)
forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *rightBarButton =
[[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[self.navigationItem setRightBarButtonItem:rightBarButton animated:YES];
直接放入 view controller 會發現沒有導航列——因為 tab bar 管理的是「並列」,而導航列來自 navigation controller。正確做法是先各自包一層:
// AppDelegate.m
self.firstNavigationController =
[[UINavigationController alloc] initWithRootViewController:self.firstViewController];
self.secondNavigationController =
[[UINavigationController alloc] initWithRootViewController:self.secondViewController];
NSArray *twoNavigationController =
[[NSArray alloc] initWithObjects:self.firstNavigationController,
self.secondNavigationController, nil];
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setViewControllers:twoNavigationController];
[self.window addSubview:self.tabBarController.view];
最後設定各頁在 tab bar 上的圖示與文字:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self != nil) {
self.title = @"First";
self.tabBarItem.image = [UIImage imageNamed:@"FirstTb.png"];
}
return self;
}
記住一個層級關係就能避免大部分困惑:UITabBarController 負責並列切換,UINavigationController 負責縱向堆疊,兩者可以嵌套,但順序必須是 tab 包 nav,而不是 nav 包 tab。至於 bar button item 的所有變化,本質上都是換掉 leftBarButtonItem / rightBarButtonItem 這兩個屬性的值而已。