/*
导航控制器:UINavigationController 继承自UIViewController
视图控制器:UIViewController
UINavigationController:管理UIViewController
UIViewController:管理UIView
UINavigationController-》UIViewController -》 UIView
*/
RootViewController * root = [[[RootViewController alloc] init] autorelease];
UINavigationController * nav = [[[UINavigationController alloc] initWithRootViewController:root] autorelease];
NSLog(@"nav is %p",nav);
// root.view.backgroundColor = [UIColor grayColor];
// 给视图控制器设置标题
root.title = @"Settings";
// 关联UINavigationController与UIViewController
/*
每一个导航控制器 都有一个栈 维护它的所有UIViewController
而且这个栈当中必须至少有一个视图控制器做为该栈的基栈
栈:先进后出
UINavigationController:UINavigation Bar(320*44) + CustomView(ctl.view) + UIToolbar(默认隐藏)
UINavigation Bar:backBarButtonItem(上一级ctl) + leftBarButtomItem + title(titleView) + rightBarButtonItem
*/
// 显示工具栏
// nav.toolbarHidden = NO;
// 让导航栏不透明
nav.navigationBar.translucent = NO;
// NSLog(@"%f",nav.navigationBar.frame.size.height);
self.window.rootViewController = nav;
return YES;
}
RootViewController.m
#import "ViewController1.h"
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
// label.backgroundColor = [UIColor redColor];
// [self.view addSubview:label];
// [label release];
NSLog(@"%p",self.navigationController);
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTap)];
[self.view addGestureRecognizer:tap];
[tap release];
}
- (void)myTap
{
// 视图控制器入栈
ViewController1 * ctl1 = [[ViewController1 alloc] init];
// ctl1.navigationController 为 nil
// ctl1.view.backgroundColor = [UIColor greenColor];
[self.navigationController pushViewController:ctl1 animated:YES];
NSLog(@"self %p",self.navigationController);
NSLog(@"ct1 %p",ctl1.navigationController);
// ctl1.navigationController 指向真实的物理空间
// [self presentViewController:ctl1 animated:YES completion:nil];
[ctl1 release];
}
- (void)dealloc
{
NSLog(@"%@ dealloc",[self class]);
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
[self.view addGestureRecognizer:tap];
[tap release];
self.view.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1.0];
// 每次push的视图控制器都会保存在该结构中
NSLog(@"%@",self.navigationController.viewControllers);
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
label.text = [NSString stringWithFormat:@"%d层",self.navigationController.viewControllers.count];
label.center = CGPointMake(160, 230);
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
[self.view addSubview:label];
[label release];
}
- (void)click
{
ViewController1 * ctl1 = [[ViewController1 alloc] init];
[self.navigationController pushViewController:ctl1 animated:YES];
// 不要写漏
[ctl1 release];
}