本文以一個實際範例,示範如何使用 UINavigationController 進行頁面導航,並透過代理在頁面之間雙向傳遞資料。
#import "OneAppDelegate.h"
#import "OneViewController.h"
@implementation OneAppDelegate
#pragma mark 導航控制器的代理方法
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
NSLog(@"已經顯示:%@", viewController);
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
NSLog(@"即將顯示:%@", viewController);
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// 初始化一個根控制器
OneViewController *root = [[[OneViewController alloc] init] autorelease];
UINavigationController *vc = [[[UINavigationController alloc] initWithRootViewController:root] autorelease];
vc.delegate = self;
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
@end它負責設定左右導航列項目,並在點擊右側按鈕時 push 下一個控制器、同時傳遞資料:
#import "OneViewController.h"
#import "SecondViewController.h"
@implementation OneViewController
#pragma mark - SecondViewControllerDelegate 的方法
- (void)setSecondData:(NSString *)str {
NSLog(@"從 Second 傳過來的數據:%@", str);
}
- (void)right {
SecondViewController *second = [[SecondViewController alloc] init];
second.title = @"第2個控制器";
second.delegate = self;
[second setString:@"。。。。。。。"]; // 傳遞數據給下一個控制器
[self.navigationController pushViewController:second animated:YES];
[second release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"紅色控制器";
// 設置左邊的 item
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(left)] autorelease];
// 設置右邊的 item
UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
[btn addTarget:self action:@selector(right) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
self.view.backgroundColor = [UIColor redColor];
// 設置返回鍵的文字為「返回」
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
}
@end它在頭檔案中定義了 SecondViewControllerDelegate 協議,並透過代理把資料回傳給上一頁:
#import "SecondViewController.h"
@interface SecondViewController ()
@property (nonatomic, retain) id<SecondViewControllerDelegate> delegate;
@end
@implementation SecondViewController
- (void)setString:(NSString *)string {
NSLog(@"從 One 傳過來的數據:%@", string);
}
- (void)click:(id)sender {
// 一口氣跳回根控制器
// [self.navigationController popToRootViewControllerAnimated:YES];
if ([self.delegate respondsToSelector:@selector(setSecondData:)]) {
[self.delegate setSecondData:@"12345"];
}
// 移除棧頂的控制器
[self.navigationController popViewControllerAnimated:YES];
}
@end透過 pushViewController 入堆疊、popViewControllerAnimated 出堆疊,再搭配代理協議,就實現了 UINavigationController 的頁面導航與雙向傳值。