背景

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

新建三个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="">

第二个页面

最新文章

  1. windows核心编程---第九章 同步设备IO与异步设备IO之同步IO
  2. Java多线程理解
  3. poj 3352Road Construction(无向双连通分量的分解)
  4. 避开unity的坑(转摘)
  5. hdu 3661 Assignments (贪心)
  6. 超级有用的9个PHP代码片段
  7. 2016-1-5第一个完整APP 私人通讯录的实现 1:登录界面及跳转的简单实现2
  8. 编译cordova-android代码
  9. package.json 字段全解析 share
  10. jquery实现视觉滚动--fullpage
  11. hiho #1079 : 离散化
  12. oracle的concat的用法
  13. UESTC_最少花费 2015 UESTC Training for Dynamic Programming<Problem D>
  14. Unity 之圆环算法
  15. sqlplus查看服务名
  16. Java集合详解及List源码分析
  17. Java日志框架:logback详解
  18. 连接Redis_五种数据格式
  19. 用bisect维护一个排序的序列
  20. 有趣的F-String

热门文章

  1. 3ds Max制作碗实例教程
  2. WebKit.NET-0.5简单应用(2)——音量解决方案
  3. 《Linux 进程间通信》命名管道:FIFO
  4. 洛谷 P1070 道路游戏
  5. 嵌入式(C)笔试题
  6. 11g Oracle Rac安装(基于linux6)可能出现的问题
  7. TextView超链接
  8. 微信开发出现 redirect-uri參数错误原因是设置回调页面域名不要加HTTP://
  9. wsimport 使用方法具体解释
  10. 未能加载文件或程序集“MICROSOFT.REPORTVIEWER.WEBFORMS …