ios开发之导航控制器的原理



#import "AppDelegate.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "ThirdViewController.h"

#import "ForthViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

     _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    

    [_window setBackgroundColor:[UIColor whiteColor]];

    

#pragma mark -导航控制器原理

    //=======================原理===================

    //1.导航控制器:是一个容器视图控制器,专门用来管理其他的视图控制器

    //导航控制器管理的视图控制器在导航控制器中的存储结构是栈结构;

    //2.导航控制器永远显示栈顶的那个视图视图控制器,

    //3.让一个导航控制器去管理其他视图控制器的方法:

    //(1).将视图控制器作为导航控制器的根视图控制器,

    //(2).使用导航控制器去push出来的视图控制器,也是属于导航控制器的视图控制器

   

#pragma mark -导航控制器的使用

    //=====================使用=============

    

   //创建一个视图控制器,用来作为导航控制器的根视图控制器

    FirstViewController *first =[[FirstViewController alloc]init];

    

    //如果一个视图控制器作为导航控制器的根视图控制器,那么这个视图控制器就是

    //导航控制器栈结构的容器中的一员(而且还是第一个添加的)


    

    //1.创建导航控制器对象

    //每个导航控制器必须至少管理一个视图,所以创建方法如下

    //UINavigationController : UIViewController

      UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:first];

    

    

    

    //3.添加视图控制器

    //push一个视图控制器到导航控制器中

    //导航控制器用来将视图控制器添加到栈中的方法

   // SecondViewController *second = [[SecondViewController alloc]init];

    

    //[navigationController pushViewController:second animated:YES];

    

   // ThirdViewController *third

    

    

    //4.pop一个视图控制器就会将这个视图从导航控制器中移除,前提是这个视图控制器

    //已经在这个导航控制器中,导航控制器中的根视图控制器不可以移除

    //一般这个pop方法只能写在你想要移除的视图控制器中去调用;

    

    

    

    

    //2.显示在window

    _window.rootViewController = navigationController;

    

    

    [_window makeKeyAndVisible];

    


    return YES;

}


@end


ForthViewController.m:

#import "ForthViewController.h"

#import "UIView+FJView.h"

@interface ForthViewController ()


@end


@implementation ForthViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor cyanColor];

    

    //添加一个按钮

    UIButton *button = [[UIButton alloc]initWithFrame:

                        CGRectMake(100, 100, 30, 20)];

    [button setBackgroundColor:[UIColor redColor]];

    

    [ button addTarget:self action:@selector(onClicked:)

      forControlEvents:UIControlEventTouchUpInside];

       self.title = @"第四页";

    [self.view addSubview:button];

    

}

#pragma mark - 按钮点击

- (void)onClicked:(UIButton *) btn{

    

    //1.将当前这个导航控制器最上层的视图控制器pop

    //回到上一层视图控制器

    //[self.navigationController popViewControllerAnimated:YES];

    

    //2.将当前导航控制器pop到根视图控制器

   // [self.navigationController popToRootViewControllerAnimated:YES];

 

    

    //拿到当前导航控制器管理的所有的视图控制器(导航控制器管理的视图控制器全是

    //导航控制器的子视图控制器)

    //子视图控制器数组中数组元素得顺序和视图的添加顺序一致;

    NSArray *chirlViewControllers = self.navigationController.childViewControllers;

    

   

    //添加转场动画;

    [self.view addTransitionAnimationWithDuration:1 animationType:FJ_pageUnCurl direction:FJ_LEFT];

    

    //使用转场动画和push方法的区别;一个放到导航控制器上一个只是纯展示;

    

    

    

    //3.pop到指定的视图控制器;(指定的视图控制器必须已经在导航控制器的栈结构中的对象)

    [self.navigationController popToViewController:chirlViewControllers[1] animated:YES];

    

}


@end


ThirdViewController.m

#import "ThirdViewController.h"

#import "ForthViewController.h"

@interface ThirdViewController ()


@end


@implementation ThirdViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor blueColor];

    

    //添加一个按钮

    UIButton *button = [[UIButton alloc]initWithFrame:

                        CGRectMake(100, 100, 30, 20)];

    [button setBackgroundColor:[UIColor redColor]];

    

    [ button addTarget:self action:@selector(onClicked:)

      forControlEvents:UIControlEventTouchUpInside];

       self.title = @"第三页";

    [self.view addSubview:button];

    

}

#pragma mark - 按钮点击

- (void)onClicked:(UIButton *) btn{

    

    ForthViewController *forth = [[ForthViewController alloc]init];

    [self.navigationController pushViewController:forth animated:YES];

    

}




@end


SecondViewController.m


#import "SecondViewController.h"

#import "ThirdViewController.h"

@interface SecondViewController ()


@end


@implementation SecondViewController


#pragma mark - 生命周期

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    UIButton *button  = [[UIButton alloc]initWithFrame:

                         CGRectMake(100, 100, 40, 20)];

    button.backgroundColor = [UIColor cyanColor];

    

    [button addTarget:self action:@selector(onclicked)

     forControlEvents:UIControlEventTouchDown];

    

    [self.view addSubview:button];

    

    

       self.title = @"第二页";

    [self.view setBackgroundColor:[UIColor orangeColor]];

}


#pragma mark - 按钮点击

-(void) onclicked{

    

    //pop回到上一个界面;

    //pop方法属于导航控制器;

    //pop当前这个视图控制器

   // - (UIViewController *)popViewControllerAnimated:(BOOL)animated;

   // [self.navigationController popViewControllerAnimated:YES];

    ThirdViewController *third = [[ThirdViewController alloc]init];

    [self.navigationController pushViewController:third animated:YES];

    

    

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

FirstViewController.m



#import "FirstViewController.h"

#import "SecondViewController.h"

@interface FirstViewController ()


@end


@implementation FirstViewController


#pragma mark -生命周期


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor lightGrayColor];

    

    //添加一个按钮

    UIButton *button = [[UIButton alloc]initWithFrame:

                        CGRectMake(100, 100, 30, 20)];

    [button setBackgroundColor:[UIColor redColor]];

    

    [ button addTarget:self action:@selector(onClicked:)

               forControlEvents:UIControlEventTouchUpInside];

    

    self.title = @"第一页";

    

    [self.view addSubview:button];

    

}

#pragma mark - 按钮点击

- (void)onClicked:(UIButton *) btn{

    

    //创建一个需要push的视图控制器对象

    SecondViewController *second = [[SecondViewController alloc]init];

    

    //只有这个视图控制器添加到导航控制器上,才可以拿到导航控制器

    //使用当前这个导航控制器push一个新的视图控制器;

    

    [self.navigationController pushViewController:second 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











转载于:https://my.oschina.net/luhoney/blog/659645

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值