背景

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

新建三个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. Mojo C++ Bindings API

    This document is a subset of the Mojo documentation. Contents Overview Getting Started Interfaces Ba ...

  2. ansible搭建mysql主主模式

    ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)等优点,实现了批量系统配置.批量程序部署.批量运行命 ...

  3. jQuery第三课 点击按钮 弹出层div效果

    jQuery 事件方法 事件方法会触发匹配元素的事件,或将函数绑定到所有匹配元素的某个事件. 触发实例: $("button#demo").click() 上面的例子将触发 id= ...

  4. 《virtual san 最佳实践》节选 Virtual SAN的发展与现状

    Virtual SAN的发展与现状Virtual SAN已经迭代更新到第四代,即Virtual SAN 6.2.通过三次主版本迭代,Virtual SAN已经成为一款非常成熟的软件定义存储软件.在此, ...

  5. 紫书 习题 8-2 UVa 1610 (暴力出奇迹)

    这道题我真的想的非常的复杂, 拿草稿纸一直在找规律,推公式, 然后总有一些特殊的情况. 然后就WA了N次.无奈之下看了别人的博客, 然后就惊了.直接暴力枚举两个相邻字符串 里面的所有可能就可以了--真 ...

  6. ActiveMQ学习总结(9)——Linux中安装ActiveMQ

    1.新建一个文件夹activeMQ   mkdir /server 2.授权    chmod 777 /server 3.下载activeMQ安装包,拷贝到/activeMQ目录下 apache-a ...

  7. spark一些入门资料

    spark一些入门资料 A Scala Tutorial for Java Programmers http://docs.scala-lang.org/tutorials/scala-for-jav ...

  8. Ordered Broadcast有序广播

    sendBroadcast()发生无序广播 sendOrderedBroadcast()发送有序广播 activity_main.xml <LinearLayout xmlns:android= ...

  9. java 经常使用測试框架

    1. 经常使用单元化測试框架 junit4 , TestNG 能够通过注解 @Before @After @BeforeClass @AfterClass 分别作方法与类级的初始化与结束动作. tes ...

  10. leetcode笔记:Merge Sorted Array

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