今天學習的是導航控制器(UINavigationController)
首先要知道的是導航控制器就是控制一組控制器的控制器:
1.自定義:需要用另外一個控制器來初始化
導航控制器的管理控制器的方法是依據棧來管理,初始化的控制器相當於入棧了
RootViewController *rootVC = [[RootViewController alloc] init];
// 初始化導航控制器
// 初始化 需要一個跟控制器 去管理(顯示)
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:rootVC];
2.需要把導航控制器作爲window的根視圖控制器
// 只要是UIConeroller的子類 都可以設置爲window的根視圖控制器
self.window.rootViewController = navC;
3.轉化到另外的視圖:例如點擊一個按鈕轉換到下一個視圖
// 下一界面按鈕
- (void)buttonClick:(UIButton *)button {
GreenViewController *greenVC = [[GreenViewController alloc] init];
// 跳轉方法
// 視圖控制器中有一個導航控制器的屬性
// 一旦這個視圖控制器被導航控制管理了
// 那麼視圖控制器中的 導航控制器屬性 就會有值
// 並且這個值 就是管理他的哪個導航控制器
[self.navigationController pushViewController:greenVC animated:YES];
[greenVC release];
}
4.跳轉頁面的方法有以下幾種常用的:
( 1 )直接跳回上一界面
[self.navigationController popViewControllerAnimated:YES];
( 2 )跳回指定界面
// 使用指定返回的頁面方法返回第二個頁面
[self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
( 3 )返回根視圖界面
[self.navigationController popToRootViewControllerAnimated:YES];
要注意的是: 跳過界面返回 中間跳過的界面也會出棧
5.導航欄
導航條 44,狀態欄 20,導航條高就是44 ,狀態欄的背景是,導航條延伸過去的
( 1 )設置導航條的顏色:
self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
( 2 )設置背景圖片
橫屏狀態下 導航條高 32
1.圖片高度 < 44 效果:平鋪
2.圖片高度 = 44 效果:只覆蓋導航條,露出狀態欄
3.圖片高度 > 44 && < 64 效果:平鋪
4.圖片高度 = 64 效果:剛好覆蓋導航條與狀態欄
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"32064"] forBarMetrics:(UIBarMetricsDefault)];
( 3 )半透明設置
iOS7.0之後 導航條默認是半透明的
起始點從屏幕的左上角,開始計算,關閉半透明後 起始點從導航條下面 開始計算(有圖片時就算不關閉半透明 也是默認從導航條下面開始計算)
self.navigationController.navigationBar.translucent = NO;
6.導航欄上的按鈕
導航條上的按鈕 使用 UIBarButtonItem
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Go" style:(UIBarButtonItemStylePlain) target:self action:@selector(barButtonItemClick:)];
// 設置左按鈕
self.navigationItem.leftBarButtonItem = barButton;
[barButton release];
// 用系統樣式的初始化方法設置左按鈕
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemCamera) target:self action:@selector(barButtonItemClick:)];
self.navigationItem.leftBarButtonItem = leftBarButton;
[leftBarButton release];
// 設置右按鈕
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"right"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBarButtonItemClick:)];
self.navigationItem.rightBarButtonItem = rightBarButton;
[rightBarButton release];
// 設置頁面的導航欄 標題
self.navigationItem.title = @"首頁";
// 設置一個view在標題位置
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 150, 44)];
titleView.backgroundColor = [UIColor cyanColor];
self.navigationItem.titleView = titleView;
[titleView release];
7.界面之間的傳值
( 1 )明確傳值的位置(從前往後)
一般從前往後用 屬性傳值
從後往前 代理傳值
後面的如何拿到前面的值? 後面可以將那個值設置爲他的屬性接收第一個界面要傳的值:
步驟:
1.先獲取到要傳的值
2.在第二個頁面 聲明一個屬性 這個屬性用來接收傳過來的值
拿從一個圖的textField裏輸入的值來傳到下一個界面CyanViewController的textField中來說:(需要把傳遞的值聲明成CyanViewController的屬性,最後再賦值給CyanViewController的textField.text)
UITextField *textField = (UITextField *)[self.view viewWithTag:1000];
CyanViewController *cyanVC = [[CyanViewController alloc] init];
cyanVC.textFieldText = textField.text;
( 2 )明確傳值的位置(從後往前)
從後往前 代理傳值
相當於讓代理去幹活
在CyanViewController中實現這個方法
UITextField *textField = (UITextField *)[self.view viewWithTag:1000];
// 安全判斷
if ([_delegate respondsToSelector:@selector(changeTextFieldText:)]) {
// 注意上面的屬性不可以直接在這裏使用 因爲上面屬性是用來接收然後給textFiled.text賦值的
[_delegate changeTextFieldText:textField.text];
}
在原視圖實現遵循協議實現的方法
協議
@protocol CyanViewControllerDelgate <NSObject>
// 聲明一個方法 用於傳值
// 傳的這個值就是方法的參數
- (void)changeTextFieldText:(NSString *)str;
實現的方法:
- (void)changeTextFieldText:(NSString *)str {
UITextField *textField = (UITextField *)[self.view viewWithTag:1000];
textField.text = str;
}
實現方法前需要設置代理:委託人在哪裏被創建就在哪裏設置代理.
這就是今天學習的所有方法,希望對你有幫助