//应用程序启动完毕即会调用15- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions16{17self.window =[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];18self.window.backgroundColor =[UIColor whiteColor];192021//3.添加子控制器到导航控制器中22//第一种也是最常用的一种23//YYOneViewController *one=[[YYOneViewController alloc]init];24//UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:one];2526//1.创建一个导航控制器27UINavigationController *nav=[[UINavigationController alloc]init];28//2.设置导航控制器为window的根视图29self.window.rootViewController=nav;3031//第二种32YYOneViewController *one =[[YYOneViewController alloc] init];33[nav pushViewController:one animated:YES];3435//第三种36//[nav addChildViewController:one];37//第四种(添加到导航控制器的栈中)38//nav.viewControllers=@[one];3940//导航控制器的栈41//nav.viewControllers;== nav.childViewControllers;42//注意该属性是只读的,因此不能像下面这样写43//nav.childViewControllers = @[one];444546[self.window makeKeyAndVisible];47returnYES;48}4950@end
YYOneViewController.m文件
1//2//YYOneViewController.m3//01-导航控制器的使用14//5//Created by apple on 14-6-4.6//Copyright (c) 2014年 itcase. All rights reserved.7//89#import"YYOneViewController.h"10#import"YYTwoViewController.h"1112@interfaceYYOneViewController ()13/**14跳转到第二个界面15*/16- (IBAction)jump2two:(id)sender;1718@end1920@implementationYYOneViewController212223- (IBAction)jump2two:(id)sender {24//1.创建第二个子控制器25YYTwoViewController *two=[[YYTwoViewController alloc]init];2627//2.把子控制器添加到导航控制器中28//有什么办法能够拿到导航控制器?29//只要当前控制器是导航控制器的子控制器,那么就可以通过该属性直接获取到当前控制器所在的导航控制器30[self.navigationController pushViewController:two animated:YES];31}3233-(void)viewDidLoad34{35[super viewDidLoad];36//控制当前控制器对应的导航条显示的内容37self.navigationItem.title=@"第一个界面";38//修改返回按钮显示的内容39self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回一"style:UIBarButtonItemStylePlain target:nil action:nil];40}41@end
YYTwoViewController.m文件
1//2//YYTwoViewController.m3//01-导航控制器的使用14//5//Created by apple on 14-6-4.6//Copyright (c) 2014年 itcase. All rights reserved.7//89#import"YYTwoViewController.h"10#import"YYThreeViewController.h"11@interfaceYYTwoViewController ()12- (IBAction)jump2Three:(id)sender;1314@end1516@implementationYYTwoViewController1718//跳转到第三个子控制器19- (IBAction)jump2Three:(id)sender {20//1.创建第三个子控制器21YYThreeViewController *three=[[YYThreeViewController alloc]init];22//2.将子控制器添加到导航控制器中23[self.navigationController pushViewController:three animated:YES];24}2526-(void)viewDidLoad27{28[super viewDidLoad];29//给导航栏添加按钮30//添加导航栏左边的按钮(添加一个相机图标的按钮),会盖掉返回31//self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];3233//为导航栏在右边添加多个按钮34//创建两个按钮35UIBarButtonItem *a=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:nil action:nil];36UIBarButtonItem *b=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:nil action:nil];37UIBarButtonItem *c=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];38self.navigationItem.rightBarButtonItems=@[a,b,c];3940//设置对应的导航条的返回(第三个界面导航条的返回)41self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回"style:UIBarButtonItemStyleBordered target:nil action:nil];42}43@end
YYThreeViewController.m文件
1//2//YYThreeViewController.m3//01-导航控制器的使用14//5//Created by apple on 14-6-4.6//Copyright (c) 2014年 itcase. All rights reserved.7//89#import"YYThreeViewController.h"10#import"YYTwoViewController.h"1112@interfaceYYThreeViewController ()13//返回到第二个控制器页面14- (IBAction)jump2two:(id)sender;15//返回到第一个控制器页面16- (IBAction)jump2root:(id)sender;1718@end1920@implementationYYThreeViewController212223- (IBAction)jump2two:(id)sender {24//跳转到第二个界面(移除当前栈顶的控制器)25[self.navigationController popViewControllerAnimated:YES];26}2728- (IBAction)jump2root:(id)sender {29//移除处理栈底控制器之外的所有控制器30[self.navigationController popToRootViewControllerAnimated:YES];3132//只要传入栈中的某一个控制器,就会跳转到指定控制器33//不能这样,没添加到导航控制器YYTwoViewController *two = [[YYTwoViewController alloc] init];34//[self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];35}36@end
实现效果:
三、导航控制器通过栈来管理子控制器
示意图
:
说明:
导航控制器是通过栈的形式来管理子控制器的(先进后出)
显示在导航控制器上得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:[UITableViewCellclass]])return@"";//1.初始化NSMutableString *xml = [NSMutableStringstring];//2.标签开头[xml appendFormat:@"<%@ frame=\"%@\"", view.class, NSStringFromCGRect(view.frame)];if(!CGPointEqualToPoint(view.bounds.origin, CGPointZero)) {
[xml appendFormat:@"bounds=\"%@\"", NSStringFromCGRect(view.bounds)];
}if([view isKindOfClass:[UIScrollViewclass]]) {
UIScrollView*scroll = (UIScrollView *)view;if(!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, scroll.contentInset)) {
[xml appendFormat:@"contentInset=\"%@\"", NSStringFromUIEdgeInsets(scroll.contentInset)];
}
}//3.判断是否要结束if(view.subviews.count ==0) {
[xml appendString:@"/>"];returnxml;
}else{
[xml appendString:@">"];
}//4.遍历所有的子控件for(UIView *childinview.subviews) {
NSString*childXml =[self digView:child];
[xml appendString:childXml];
}//5.标签结尾[xml appendFormat:@"", view.class];returnxml;
}