又到了整理學習筆記的時候。時間過得很快,總覺得不太夠用,但零基礎並不代表什麼,願意擠時間就總會有收穫。今天要談的是 UINavigationController。
兩個步驟:先建立導航控制器並指定它的 RootViewController,再把導航控制器設為 window 的根控制器。
// 步驟一:建立導航控制器,並指定根控制器
FirstViewController *firstVC = [[FirstViewController alloc] init];
NSLog(@"第一個頁面初始化完成");
UINavigationController *navC =
[[UINavigationController alloc] initWithRootViewController:firstVC];
[firstVC release];
NSLog(@"設定第一個頁面為導航控制器的 rootViewController");
// 步驟二:把導航控制器設為 window 的根控制器
self.window.rootViewController = navC;
NSLog(@"設定導航控制器為 window 的 RootViewController");
[navC release];
UINavigationController 是「控制器的控制器」,同樣繼承自 UIViewController,負責管理一組視圖控制器。被管理的控制器以堆疊(後進先出)的形式存放,push 入堆疊、pop 出堆疊。
每個導航控制器自帶一個 navigationBar,而 UINavigationBar 繼承自 UIView。它負責導航條的呈現——iOS 7 之前是不透明的,之後改為預設透明。
// 是否半透明(設了背景色時半透明會失效) naviVC.navigationBar.translucent = YES; // 導航條上 item 的顏色 naviVC.navigationBar.tintColor = [UIColor purpleColor]; // 導航條樣式 naviVC.navigationBar.barStyle = UIBarStyleBlack;
今天練習到的方式有屬性傳值與單例傳值兩種。
單例的建立有三個原則:是類別方法、回傳值型別為當前類別、方法名為 default + 類別名。
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
self.navigationItem.title = @"主頁";
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 60, 30)];
label.text = @"用戶名";
label.textAlignment = NSTextAlignmentRight;
[self.view addSubview:label];
[label release];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 60, 30)];
label1.text = @"性別";
label1.textAlignment = NSTextAlignmentRight;
[self.view addSubview:label1];
[label1 release];
field = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
field.borderStyle = UITextBorderStyleRoundedRect;
field.placeholder = @"請輸入用戶名";
field.autocorrectionType = UITextAutocorrectionTypeNo;
field.spellCheckingType = UITextSpellCheckingTypeNo;
[self.view addSubview:field];
[field release];
field1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 30)];
field1.borderStyle = UITextBorderStyleRoundedRect;
field1.placeholder = @"請輸入性別";
[self.view addSubview:field1];
[field1 release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(70, 200, 70, 30);
[self.view addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(180, 200, 70, 30);
[button1 addTarget:self action:@selector(changPage:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:@"取消" forState:UIControlStateNormal];
[self.view addSubview:button1];
// 點擊背景時收起鍵盤
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnThekeyBorld:)];
[self.view addGestureRecognizer:tap];
[tap release];
}
// 讓所有 UITextField 結束編輯
- (void)returnThekeyBorld:(UITapGestureRecognizer *)tap {
for (UIView *view in self.view.subviews) {
if ([view class] == [UITextField class]) {
[view resignFirstResponder];
}
}
}
// 把輸入值存入單例,再 push 到詳情頁
- (void)changPage:(UIButton *)button {
Single *single = [Single defaultSingle];
single.nameString = field.text;
NSLog(@"%@", single.nameString);
HomeDetailViewController *detail = [[HomeDetailViewController alloc] init];
detail.nameString = [NSString stringWithFormat:@"歡迎 %@ 性別 %@ 登陸成功",
field.text, field1.text];
[self.navigationController pushViewController:detail animated:YES];
[detail release];
}
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor redColor];
[super viewDidLoad];
self.navigationItem.title = @"詳情頁";
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 200, 100)];
label.numberOfLines = 0;
label.center = self.view.center;
label.textAlignment = NSTextAlignmentCenter;
// 透過單例取值
Single *single = [Single defaultSingle];
label.text = single.nameString;
[self.view addSubview:label];
[label release];
}
// Single.h #import <Foundation/Foundation.h> @interface Single : NSObject @property (nonatomic, retain) NSString *nameString, *genderString; // 單例建立三原則:類別方法、回傳當前類別、方法名 default + 類別名 + (Single *)defaultSingle; @end
// Single.m
#import "Single.h"
@implementation Single
+ (Single *)defaultSingle {
static Single *single = nil;
if (single == nil) {
single = [[Single alloc] init];
}
return single;
}
- (void)dealloc {
self.nameString = nil;
self.genderString = nil;
[super dealloc];
}
@end
屬性傳值是首選:先在目標控制器宣告屬性,push 之前指派即可,關係明確、資料流清楚,也方便測試。
單例傳值雖然寫起來快,但它把資料變成全域狀態,多個頁面共用同一份時容易互相干擾,除錯時也難以追蹤是誰改了值。建議只用於真正需要全域共享的情境,例如登入狀態、使用者設定,而不是拿來傳遞一次性的頁面參數。