您的位置:首页 > 运维架构

导航控制器push/pop

2014-10-29 15:31 162 查看
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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  push pop
相关文章推荐