IOS开发之导航控制器原理



#import "AppDelegate.h"数组

#import "FirstViewController.h"app

#import "SecondViewController.h"ide

#import "ThirdViewController.h"动画

#import "ForthViewController.h"url

@interface AppDelegate ()spa


@end .net


@implementation AppDelegate3d



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

    // Override point for customization after application launch.orm

    

     _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


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.

}


@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


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




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

相关文章
相关标签/搜索