UINavigationController可以实现栈的方式管理ViewContoroller
1、把SupportingFiles 中 info.plist中的Main storyboard file base name置为空,并新建几个Cocos Touch 文件,继承自UIViewContoroller,名字分别为 ViewContoroller2, ViewContoroller3 ,详见下图
2、AppDeleggate中初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//创建window。大小为screen大小
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//创建根UIViewController
UIViewController* rootVc=[[ViewController alloc]init];
//创建UINavigationController,并用ViewController初始化
UINavigationController* nc=[[UINavigationController alloc]initWithRootViewController:rootVc];
//设置rootViewController为自定义UINavigationController
self.window.rootViewController=nc;
//设置显示
[self.window makeKeyAndVisible];
return YES;
}
3、ViewController中代码如下
#import "ViewController.h"
#import "ViewController2.h"
@interface ViewController ()
@end
@implementation ViewController
//设置背景,添加按钮,并添加点击事件
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor redColor];
// Do any additional setup after loading the view, typically from a nib.
UIButton* but=[[UIButton alloc]initWithFrame:CGRectMake(50, 80, 100, 50)];
[but setTitle:@"开启" forState:UIControlStateNormal];
but.backgroundColor=[UIColor grayColor];
[self.view addSubview:but];
[but addTarget:self action:@selector(open ) forControlEvents:UIControlEventTouchUpInside];
}
-(void)open {
//当该UIViewController由<span>UINavigationController</span>管理时,self.navigationController才不为nil
//把要开启的UIViewController作为参数传给pushViewController来开启新界面
[self.navigationController pushViewController:[[ViewController2 alloc]init] animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4、ViewController2代码如下,可以开启新界面,也可以关闭当前界面
#import "ViewController2.h"
#import "ViewController3.h"
@interface ViewController2 ()
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor greenColor];
UIButton* but=[[UIButton alloc] initWithFrame:CGRectMake(10, 100, 100, 80)];
[but setTitle:@"开启3" forState:UIControlStateNormal];
but.backgroundColor=[UIColor purpleColor];
[but addTarget:self action:@selector(openNext) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
/返回按钮///
UIButton*butBack =[[UIButton alloc]initWithFrame:CGRectMake(10, 200, 150, 60)];
[butBack setTitle:@"返回" forState:UIControlStateNormal];
butBack.backgroundColor=[UIColor darkGrayColor];
[butBack addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:butBack];
// Do any additional setup after loading the view.
}
-(void)close{
//返回上一级
[self.navigationController popViewControllerAnimated:YES];
}
-(void )openNext{
[self.navigationController pushViewController:[[ViewController3 alloc]init] animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
5、ViewController3回到根UIViewController
#import "ViewController3.h"
@interface ViewController3 ()
@end
@implementation ViewController3
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor blueColor];
UIButton* but=[[UIButton alloc] initWithFrame:CGRectMake(10, 100, 100, 80)];
[but setTitle:@"回到根" forState:UIControlStateNormal];
but.backgroundColor=[UIColor purpleColor];
[but addTarget:self action:@selector(gotoRoot) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
// Do any additional setup after loading the view.
}
-(void)gotoRoot{
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end