IOS開發UI篇—導航控制器屬性和基本使用
一、導航控制器的一些屬性和基本使用
1.把子控制器添加到導航控制器中的四種方法
(1)
1.建立一個導航控制器
UINavigationController *nav=[[UINavigationControlleralloc]init];
2.設定導航控制器為window的根視圖
self.window.rootViewController=nav;
3.添加
YYOneViewController *one = [[YYOneViewController alloc] init];
[nav pushViewController:one animated:YES];
(2)
UINavigationController *nav=[[UINavigationControlleralloc]init];
2.設定導航控制器為window的根視圖
self.window.rootViewController=nav;
3.添加
YYOneViewController *one = [[YYOneViewController alloc] init];
[nav addChildViewController:one];
(3)
nav.viewControllers=@[one];(添加到導航控制器的棧中)
說明:nav.viewControllers;== nav.childViewControllers;注意該屬性是隻讀的,是以不能像下面這樣寫。nav.childViewControllers = @[one];
(4)最常用的方法
YYOneViewController *one=[[YYOneViewController alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:one];
2.目前子控制器界面導航欄的标題以及對應傳回标題的設定
self.navigationItem.title=@"第一個界面";
self.navigationItem.backBarButtonItem=[[UIBarButtonItemalloc]initWithTitle:@"傳回一" style:UIBarButtonItemStylePlain target:nilaction:nil];
3.給導航欄添加按鈕
說明:可添加一個,也可以添加多個(數組)
添加導航欄左邊的按鈕(添加一個相機圖示的按鈕),會蓋掉傳回
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
4.界面跳轉
跳轉到第二個界面(目前為第三個,移除目前棧頂的控制器) [self.navigationControllerpopViewControllerAnimated:YES];
移除處理棧底控制器之外的所有控制器 [self.navigationControllerpopToRootViewControllerAnimated:YES];
隻要傳入棧中的某一個控制器,就會跳轉到指定控制器 [self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];
二、代碼示例
YYAppDelegate.m檔案
1 //
2 // YYAppDelegate.m
3 // 01-導航控制器的使用1
4 //
5 // Created by apple on 14-6-4.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYAppDelegate.h"
10 #import "YYOneViewController.h"
11
12 @implementation YYAppDelegate
13
14 //應用程式啟動完畢即會調用
15 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
16 {
17 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
18 self.window.backgroundColor = [UIColor whiteColor];
19
20
21 //3.添加子控制器到導航控制器中
22 //第一種也是最常用的一種
23 // YYOneViewController *one=[[YYOneViewController alloc]init];
24 // UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:one];
25
26 //1.建立一個導航控制器
27 UINavigationController *nav=[[UINavigationController alloc]init];
28 //2.設定導航控制器為window的根視圖
29 self.window.rootViewController=nav;
30
31 //第二種
32 YYOneViewController *one = [[YYOneViewController alloc] init];
33 [nav pushViewController:one animated:YES];
34
35 //第三種
36 // [nav addChildViewController:one];
37 // 第四種(添加到導航控制器的棧中)
38 // nav.viewControllers=@[one];
39
40 // 導航控制器的棧
41 // nav.viewControllers;== nav.childViewControllers;
42 // 注意該屬性是隻讀的,是以不能像下面這樣寫
43 // nav.childViewControllers = @[one];
44
45
46 [self.window makeKeyAndVisible];
47 return YES;
48 }
49
50 @end
YYOneViewController.m檔案
1 //
2 // YYOneViewController.m
3 // 01-導航控制器的使用1
4 //
5 // Created by apple on 14-6-4.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYOneViewController.h"
10 #import "YYTwoViewController.h"
11
12 @interface YYOneViewController ()
13 /**
14 跳轉到第二個界面
15 */
16 - (IBAction)jump2two:(id)sender;
17
18 @end
19
20 @implementation YYOneViewController
21
22
23 - (IBAction)jump2two:(id)sender {
24 //1.建立第二個子控制器
25 YYTwoViewController *two=[[YYTwoViewController alloc]init];
26
27 //2.把子控制器添加到導航控制器中
28 //有什麼辦法能夠拿到導航控制器?
29 //隻要目前控制器是導航控制器的子控制器,那麼就可以通過該屬性直接擷取到目前控制器所在的導航控制器
30 [self.navigationController pushViewController:two animated:YES];
31 }
32
33 -(void)viewDidLoad
34 {
35 [super viewDidLoad];
36 //控制目前控制器對應的導覽列顯示的内容
37 self.navigationItem.title=@"第一個界面";
38 //修改傳回按鈕顯示的内容
39 self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"傳回一" style:UIBarButtonItemStylePlain target:nil action:nil];
40 }
41 @end
YYTwoViewController.m檔案
1 //
2 // YYTwoViewController.m
3 // 01-導航控制器的使用1
4 //
5 // Created by apple on 14-6-4.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYTwoViewController.h"
10 #import "YYThreeViewController.h"
11 @interface YYTwoViewController ()
12 - (IBAction)jump2Three:(id)sender;
13
14 @end
15
16 @implementation YYTwoViewController
17
18 //跳轉到第三個子控制器
19 - (IBAction)jump2Three:(id)sender {
20 //1.建立第三個子控制器
21 YYThreeViewController *three=[[YYThreeViewController alloc]init];
22 //2.将子控制器添加到導航控制器中
23 [self.navigationController pushViewController:three animated:YES];
24 }
25
26 -(void)viewDidLoad
27 {
28 [super viewDidLoad];
29 //給導航欄添加按鈕
30 //添加導航欄左邊的按鈕(添加一個相機圖示的按鈕),會蓋掉傳回
31 // self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
32
33 //為導航欄在右邊添加多個按鈕
34 //建立兩個按鈕
35 UIBarButtonItem *a=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:nil action:nil];
36 UIBarButtonItem *b=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:nil action:nil];
37 UIBarButtonItem *c=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
38 self.navigationItem.rightBarButtonItems=@[a,b,c];
39
40 //設定對應的導覽列的傳回(第三個界面導覽列的傳回)
41 self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"傳回" style:UIBarButtonItemStyleBordered target:nil action:nil];
42 }
43 @end
YYThreeViewController.m檔案
1 //
2 // YYThreeViewController.m
3 // 01-導航控制器的使用1
4 //
5 // Created by apple on 14-6-4.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYThreeViewController.h"
10 #import "YYTwoViewController.h"
11
12 @interface YYThreeViewController ()
13 //傳回到第二個控制器頁面
14 - (IBAction)jump2two:(id)sender;
15 //傳回到第一個控制器頁面
16 - (IBAction)jump2root:(id)sender;
17
18 @end
19
20 @implementation YYThreeViewController
21
22
23 - (IBAction)jump2two:(id)sender {
24 //跳轉到第二個界面(移除目前棧頂的控制器)
25 [self.navigationController popViewControllerAnimated:YES];
26 }
27
28 - (IBAction)jump2root:(id)sender {
29 //移除處理棧底控制器之外的所有控制器
30 [self.navigationController popToRootViewControllerAnimated:YES];
31
32 // 隻要傳入棧中的某一個控制器,就會跳轉到指定控制器
33 //不能這樣,沒添加到導航控制器YYTwoViewController *two = [[YYTwoViewController alloc] init];
34 //[self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];
35 }
36 @end
實作效果:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuQDN1ATOjJWY0QmY5gTOkdDO0EzYzgTZyYmY4YmNlhDNfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
三、導航控制器通過棧來管理子控制器
示意圖
:
說明:
導航控制器是通過棧的形式來管理子控制器的(先進後出)
顯示在導航控制器上得view永遠是棧頂控制器的view
一個導航控制器隻有一個導覽列,也就是說所有的自控制器公用一個導覽列。
四、補充
在代理方法中,列印目前window下面的所有子控件,并通過xml檔案來儲存,代碼如下。
// 應用程式擷取焦點(代表着可以和使用者互動)
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"applicationDidBecomeActive");
UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
UINavigationBar *bar = nav.navigationBar;
// NSLog(@"%@", NSStringFromCGRect(bar.frame));
NSString *str = [self digView:self.window];
[str writeToFile:@"/Users/apple/Desktop/ios6.xml" atomically:YES];
}
/**
* 傳回傳入veiw的所有層級結構
*
* @param view 需要擷取層級結構的view
*
* @return 字元串
*/
- (NSString *)digView:(UIView *)view
{
if ([view isKindOfClass:[UITableViewCell class]]) return @"";
// 1.初始化
NSMutableString *xml = [NSMutableString string];
// 2.标簽開頭
[xml appendFormat:@"<%@ frame=\"%@\"", view.class, NSStringFromCGRect(view.frame)];
if (!CGPointEqualToPoint(view.bounds.origin, CGPointZero)) {
[xml appendFormat:@" bounds=\"%@\"", NSStringFromCGRect(view.bounds)];
}
if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView *scroll = (UIScrollView *)view;
if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, scroll.contentInset)) {
[xml appendFormat:@" contentInset=\"%@\"", NSStringFromUIEdgeInsets(scroll.contentInset)];
}
}
// 3.判斷是否要結束
if (view.subviews.count == 0) {
[xml appendString:@" />"];
return xml;
} else {
[xml appendString:@">"];
}
// 4.周遊所有的子控件
for (UIView *child in view.subviews) {
NSString *childXml = [self digView:child];
[xml appendString:childXml];
}
// 5.标簽結尾
[xml appendFormat:@"</%@>", view.class];
return xml;
}
注意:在ios7和以前版本中,各個控件,包括子控制器界面frame的不同。