背景

有一个根视图控制器 然后跳转到第一个界面  第一个界面能够返回到根视图 也能够跳转到第二个视图 第二个视图能够直接返回到根视图

新建三个ViewController    RootViewController FirstViewController SecondViewController

首先在AppDelegate.m中写入

#import "WJJAppDelegate.h"
#import "WJJRootViewController.h" @implementation WJJAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
WJJRootViewController * rootViewController = [[WJJRootViewController alloc] init];
//创建一个导航控制器 并把上面创建的root 加入到导航控制器上
UINavigationController * nav = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}

然后再RootViewController中写入 点击button会进入到firstViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createButton];
} //新建一个button 点击进入写一个界面
- (void)createButton{
UIButton * nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
nextButton.frame = CGRectMake(40, 80, 240, 40);
[nextButton setTitle:@"下一页" forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nextButton];
} - (void)nextPage{
WJJFirstViewController * first = [[WJJFirstViewController alloc] init];
//利用push方法 进入下一个界面 当返回上一个界面的时候 全部界面并不没有了 而是压到栈中
//跟之前present那个跳转界面是不同的
[self.navigationController pushViewController:first animated:YES];
}

在firstViewController中 点击左边按钮能够返回首页 点击右边按钮 进入下一页

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
//在导航控制器的左边、右边自己定义返回键、下一页键
[self createBarButtonItem];
} - (void)createBarButtonItem{ UIButton * popButton = [UIButton buttonWithType:UIButtonTypeSystem];
//假设放在导航栏的左右、自己定义的button跟x、y无关 仅仅跟宽高有关系
popButton.frame = CGRectMake(0, 0, 50, 20);
//设置标题
[popButton setTitle:@"返回" forState:UIControlStateNormal];
[popButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//以popbutton创建一个自己定义的导航条button
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:popButton];
//让导航栏的返回button 换成我们自己定义的
self.navigationItem.leftBarButtonItem = item; UIButton * pushButton = [UIButton buttonWithType:UIButtonTypeSystem];
//假设放在导航栏的左右、自己定义的button跟x、y无关 仅仅跟宽高有关系
pushButton.frame = CGRectMake(0, 0, 50, 20);
//设置标题
[pushButton setTitle:@"下一页" forState:UIControlStateNormal];
[pushButton addTarget:self action:@selector(nextPage) forControlEvents:UIControlEventTouchUpInside];
//以pushbutton创建一个自己定义的导航条button
UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithCustomView:pushButton];
//让导航栏的右边button 换成我们自己定义的
self.navigationItem.rightBarButtonItem = item2; } - (void)nextPage{
WJJSecondViewController * second = [[WJJSecondViewController alloc] init];
[self.navigationController pushViewController:second animated:YES];
} - (void)back{
//pop即是把此视图压到栈里面 让上一个界面显示
[self.navigationController popToRootViewControllerAnimated:YES];
}

然后,在secondViewController中点击button 返回首页

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
[self createToRootButton];
} //新建一个button 点击返回首页
- (void)createToRootButton{
UIButton * toRootButton = [UIButton buttonWithType:UIButtonTypeSystem];
toRootButton.frame = CGRectMake(40, 80, 240, 40);
[toRootButton setTitle:@"首页" forState:UIControlStateNormal];
[toRootButton addTarget:self action:@selector(toRootPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:toRootButton];
} //点击按钮 返回到首页 有两种方式
- (void)toRootPage{ //第一种 直接返回到首页
//[self.navigationController popToRootViewControllerAnimated:YES]; //另外一种 由于这些视图控制器是压栈、出栈操作,所以在视图控制器里有一个视图控制器的数组 首页下标为0
[self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES]; }

首页

第一个页面

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

第二个页面

Snail—UI学习之导航视图控制器UINavigationController(系统)的更多相关文章

  1. UI 07 _ 导航视图控制器 与 属性传值

    首先, 先创建三个VC. 完毕点击按钮, 进入下一页, 并可以返回. 要先把导航视图控制器创建出来. 在AppDelegate.m 文件里代码例如以下: #import "AppDelega ...

  2. ios 导航视图控制器 跳转

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  3. iOS学习22之视图控制器

    1.自定义视图 1> 概述   定义视图:系统标准UI之外,自己组合而出的新的视图. 定义视图的优点: iOS提供了很多UI组件,借助它们我们可以实现不同的功能.尽管如此,实际开发中,我们还需要 ...

  4. 步步入佳境---UI入门(3) --单视图控制器

    视图控制器特点//1,抽象  视觉上没有效果//2,负责控制视图的显示方式//3,负责通知视图的显示内容//4,ios平台赋予的,收到内存警告和检测设备旋转@interface CHViewContr ...

  5. Snail—UI学习之UITextField

    简单看一下UITextField的属性 - (void)createTextField{ UITextField * textField = [[UITextField alloc] initWith ...

  6. Snail—UI学习之自己定义标签栏UITabBarController

    这里的背景跟上面的差点儿相同 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkF ...

  7. Snail—UI学习之得到某组件的方法

    第一种方法:依据传入函数的參数对象的tag属性区分 比方 多个button运行同一个方法,可是不同的方法运行时.里面的逻辑又不一样 那就得加以区分 这时能够用tag来差别 //再新建一个Button ...

  8. Snail—UI学习之自己定义通知NSNotification

    背景是:一个界面跳转到第二个界面 然后 第一个界面发了一个通知  然后第二个界面收到这个通知后 把里面的数据取出来 在RootViewController.m中写入以下代码 #import " ...

  9. [Xcode 实际操作]三、视图控制器-(4)使用UINavigationController导航栏和工具栏

    目录:[Swift]Xcode实际操作 本文将演示如何显示和隐藏导航视图的导航栏和工具栏 打开第一个视图控制器 import UIKit class FirstSubViewController: U ...

随机推荐

  1. SpringCloud学习笔记(14)----Spring Cloud Netflix之Hystrix对Feign的支持

    1. Hystrix对Feign的支持 添加Feign中IUserBiz的实现类HystrixFallBack: package com.wangx.cloud.springcloud02consum ...

  2. freemarker加载模板文件的

    java代码: public String getContent(String name, HashMap<String, Object> paramMap) { //home 文件路径 ...

  3. (三)React基础

    3-1 使用React编写TodoList功能 import { Fragment} from ‘react’ Fragment是占位符 用于替代最外层div元素, 防止生成的元素会有两层div嵌套这 ...

  4. HDU-5025 Saving Tang Monk 广度搜索 状态压缩

    题目链接:https://cn.vjudge.net/problem/HDU-5025 题意 救唐僧,路上有m(<=9)把钥匙,最多5条蛇和一个唐僧. 目标是前往唐僧的地方,用全部钥匙打开全部的 ...

  5. svn 验证位置失败 Authorization failed

    进入svn的conf目录下 修改svnserve.conf [general] anon-access = none  #未登录用户不允许访问 auth-access = write  passwor ...

  6. 洛谷 P1373 小a和uim之大逃离 (差值型dp总结)

    这道题和多米诺骨牌那道题很像 ,都是涉及到差值的问题. 这道题是二维的,同时要取模. 这种题,因为当前的决策有后效性,会影响到差值,所以直接把 差值作为维度,然后计算答案的时候把差值为0的加起来就行了 ...

  7. df -h 挂死

    df -h 卡死的情况,那是因为无法统计挂载的目录的大小 一般是因为还挂载了一些外部的目录,如nfs的目录 可以用mount | column -t 命令查看哪些目录 然后umount这些目录, 一般 ...

  8. Memcached windows安装

    Memcached windows安装 如果出现提示: Microsoft Windows [版本 6.3.9600] (c) 2013 Microsoft Corporation.保留所有权利. D ...

  9. leetcode笔记:Merge Sorted Array

    一.题目描写叙述 二.解题技巧 这道题不存在复杂的分析过程和边界条件.假设单纯得考虑从小到大地将两个数组进行合并的话.每次在num1中插入一个数的话,须要将后面的元素都向后移动一位.这样.整个处理过程 ...

  10. Thinking in States

    Thinking in States Niclas Nilsson PEOPLE IN THE REAL WORLD HAVE A WEIRD RELATIONSHIP WITH STATE. Thi ...