AppDelegate.m
#import "RootViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//创建视图控制器
RootViewController *rootCtrl = [[RootViewController alloc] init];
//创建导航控制器
//将rootCtrl交给导航控制器
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:rootCtrl];
/*
将导航控制器作为window的根控制器
导航控制器的根视图添加到window上面
*/
self.window.rootViewController = navCtrl;
[rootCtrl release];
[navCtrl release];
return YES;
}
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"第一个控制器";
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 90, 65)];
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
[view release];
self.view.backgroundColor = [UIColor grayColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 100, 100, 50);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"push" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction {
SecondViewController *secondCtrl = [[SecondViewController alloc] init];
/*
实现的原理:
1.先将rootCtrl的根视图从导航控制器的根视图上移除
2.再将secondCtrl的根视图添加到导航控制器的视图上
*/
[self.navigationController pushViewController:secondCtrl animated:YES];
[secondCtrl release];
}
- (void)dealloc
{
NSLog(@"root dealloc");
[super dealloc];
}
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
// [self.navigationController setToolbarHidden:NO];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = 101;
button.frame = CGRectMake(100, 100, 100, 50);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"pop" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
NSLog(@"%@",self.view);
[self.view addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.tag = 102;
button1.frame = CGRectMake(100, 200, 100, 50);
button1.backgroundColor = [UIColor redColor];
[button1 setTitle:@"push" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"2:%@",self.view);
[super viewDidAppear:animated];
}
- (void)buttonAction:(UIButton *)button {
if (button.tag == 101) {
//回到前一个视图控制器
[self.navigationController popViewControllerAnimated:YES];
}else if (button.tag == 102) {
ThirdViewController *thirCtrl = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thirCtrl animated:YES];
[thirCtrl release];
}
}
- (void)dealloc
{
NSLog(@"sec dealloc");
[super dealloc];
}
ThirdViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
}