UINavigationController

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


                         


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值