当前位置: 首页 > news >正文

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

2019独角兽企业重金招聘Python工程师标准>>> hot3.png



#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

相关文章:

  • Windows下安装使用curl命令
  • paip.数据库发邮件通知配置
  • 20145328 《Java程序设计》实验二实验报告
  • UNIX网络编程——经常使用的套接字选项
  • 【Mysql 学习】SQL服务器模式
  • struts-default.xml解读
  • 用POP动画编写带富文本的自定义动画效果
  • 〖Linux〗不知谁写的,很实用的Linux命令
  • 带动画渐进效果与颜色渐变的圆弧进度控件设计
  • [Android]Tool-Systrace
  • 抱歉,我不接私单了
  • Java中数据库连接池原理机制的详细讲解(转)
  • App安全之网络传输安全
  • 记录:C#编程的一点小细节
  • 8000端口下 调用pdo数据库连接的报错原因
  • 2017-09-12 前端日报
  • hadoop入门学习教程--DKHadoop完整安装步骤
  • JavaScript设计模式之工厂模式
  • mockjs让前端开发独立于后端
  • React+TypeScript入门
  • vue--为什么data属性必须是一个函数
  • Web标准制定过程
  • 马上搞懂 GeoJSON
  • 用Visual Studio开发以太坊智能合约
  • 优秀架构师必须掌握的架构思维
  • 原生 js 实现移动端 Touch 滑动反弹
  • 1.Ext JS 建立web开发工程
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • $forceUpdate()函数
  • (175)FPGA门控时钟技术
  • (2)MFC+openGL单文档框架glFrame
  • (42)STM32——LCD显示屏实验笔记
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (简单) HDU 2612 Find a way,BFS。
  • (原創) 如何刪除Windows Live Writer留在本機的文章? (Web) (Windows Live Writer)
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .Net Core与存储过程(一)
  • .net 流——流的类型体系简单介绍
  • .net反编译工具
  • .Net转Java自学之路—SpringMVC框架篇六(异常处理)
  • .vue文件怎么使用_我在项目中是这样配置Vue的
  • /usr/bin/python: can't decompress data; zlib not available 的异常处理
  • [ vulhub漏洞复现篇 ] struts2远程代码执行漏洞 S2-005 (CVE-2010-1870)
  • [ 云计算 | AWS 实践 ] 基于 Amazon S3 协议搭建个人云存储服务
  • []error LNK2001: unresolved external symbol _m
  • [20160807][系统设计的三次迭代]
  • [3300万人的聊天室] 作为产品的上游公司该如何?
  • [APIO2012] 派遣 dispatching
  • [BZOJ] 2006: [NOI2010]超级钢琴
  • [BZOJ1178][Apio2009]CONVENTION会议中心
  • [Design Pattern] 工厂方法模式
  • [idea]关于idea开发乱码的配置
  • [IE编程] 打开/关闭IE8的光标浏览模式(Caret Browsing)
  • [NYOJ 536] 开心的mdd