標籤:
一、導航控制器的一些屬性和基本使用
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)
1.建立一個導航控制器
UINavigationController *nav=[[UINavigationControlleralloc]init];
2.設定導航控制器為window的根視圖
self.window.rootViewController=nav;
3.添加
YYOneViewController *one = [[YYOneViewController alloc] init];
[nav addChildViewController:one];
(3)
1.建立一個導航控制器
UINavigationController *nav=[[UINavigationControlleralloc]init];
2.設定導航控制器為window的根視圖
self.window.rootViewController=nav;
3.添加
YYOneViewController *one = [[YYOneViewController alloc] init];
[email protected][one];(添加到導航控制器的棧中)
導航控制器有兩個屬性viewControllers與childViewControllers,數群組類型,是專門用來裝子控制器的棧
說明:nav.viewControllers;== nav.childViewControllers;注意該屬性是唯讀,因此不能像下面這樣寫。nav.childViewControllers = @[one];
(4)最常用的方法
YYOneViewController *one=[[YYOneViewController alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:one];
2.當前子控制器介面導覽列的標題以及對應返回標題的設定
[email protected]"第一個介面";
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 YYAppDelegate13 14 //應用程式啟動完畢即會調用15 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions16 {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 // [email protected][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 @end19 20 @implementation YYOneViewController21 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)viewDidLoad34 {35 [super viewDidLoad];36 //控制當前控制器對應的導航條顯示的內容37 [email protected]"第一個介面";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 @end15 16 @implementation YYTwoViewController17 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)viewDidLoad27 {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 [email protected][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 @end19 20 @implementation YYThreeViewController21 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
實現效果:
三、導航控制器通過棧來管理子控制器
:
說明:
導航控制器是通過棧的形式來管理子控制器的(先進後出)
顯示在導航控制器上得view永遠是棧頂控制器的view(就是當前顯示的view)比如當前第三個介面點擊返回,就會把當前的controller銷毀(移出棧,並且它的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;}
IOS開發UI篇—導航控制器屬性和基本使用