IOS之导航控制器
UINavigationController是用于构建分层应用程序的主要工具,主要采用栈形式来实现视图。任何类型的视图控制器都可放入栈中。在设计导航控制器时需要指定根视图即用户看到的第一个视图。根视图控制器是被导航控制器推入到栈中的第一个视图控制器。当用户查看下一个试图时,栈中将加入一个新的视图控制器,它所控制的视图将展示给用户。我们可以通过导航按钮来操作分层的应用程序,用它来控制视图的推入或推出。
1、把子控制器添加到导航控制器中常用的方法
//创建视图控制器
JRViewController * vc=[[JRViewController alloc] init]; //创建导航控制器,并且将上面的控制器作为导航控制器的根控制器
UINavigationController * naVC=[[UINavigationController alloc] initWithRootViewController:vc]; //将当前的导航控制器设置窗口的根视图控制器
self.window.rootViewController=naVC;
2、这样我们就把JRViewController作为了当前导航控制器的根控制器,下面我们进入到JRViewController,并且添加一个标题和按钮代码如下
//设置背景
self.view.backgroundColor=[UIColor redColor];
//设置标题
self.title=@"精品"; //添加按钮
UIButton * button=[[UIButton alloc] initWithFrame:CGRectMake(, , , )];
button.backgroundColor=[UIColor blackColor];
[button setTitle:@"push" forState:UIControlStateNormal];
button.center=self.view.center;
[button addTarget:self action:@selector(pushVCNew) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
效果图如下:
3、下面我们就进行视图控制器之间的跳转
导航控制器管理视图控制器主要采取压栈和出栈的方式,下面我们一起来感受一下如何切换,上面添加了按钮我们增加了点击事件,下面我们重新一下这个点击事件触发的方法:
- (void) pushVCNew{ //初始化第二个控制器
SecondViewController * sec=[[SecondViewController alloc] init];
//切换到另一个视图控制器
[self.navigationController pushViewController:sec animated:YES]; }
效果图如下:
压栈进入后响应的就有出栈,如何进行出栈返回呢,我们重写一下第二个控制器的返回方法,调用控制器的popViewControllerAnimated方法即可
- (void) popVC{
[self.navigationController popViewControllerAnimated:YES];
}
5、导航按钮
//1 构造方法
UIBarButtonItem * item1=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bt1"] style:UIBarButtonItemStylePlain target:self action:@selector(leftClick)]; //2 自定义视图
UIButton * button=[[UIButton alloc] initWithFrame:CGRectMake(, , , )];
button.backgroundColor=[UIColor redColor];
[button setTitle:@"hello" forState:UIControlStateNormal];
UIBarButtonItem * item2=[[UIBarButtonItem alloc] initWithCustomView:button]; //3 添加到当前navigationItem
self.navigationItem.leftBarButtonItem=item2;
self.navigationItem.rightBarButtonItem=item1; //4 增加标题
UILabel * label=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.text=@"导航控制器";
label.textColor=[UIColor redColor];
self.navigationItem.titleView=label;
效果图如下
6、ToolBar工具条,每个导航控制器都有一个工具条,默认为隐藏状态,下面我们将工具条打开并使用它。
//打开toolbar
self.navigationController.toolbarHidden=NO; //定义ToolBar增加按钮
UIBarButtonItem * item1=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pushVC)];
UIBarButtonItem * item2=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(pushVC)];
UIBarButtonItem * item3=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(pushVC)];
UIBarButtonItem * item4=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(pushVC)];
UIBarButtonItem * item5=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pushVC)];
UIBarButtonItem * item6=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(pushVC)]; UIBarButtonItem * item7=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(pushVC)];
UIBarButtonItem * item8=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:@selector(pushVC)];
item8.width=;
//将按钮添加到工具条
self.toolbarItems=@[item1,item8,item2,item8,item3,item8,item4,item7,item5,item8,item6];
效果图如下:
出处:http://www.cnblogs.com/jerehedu/
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询:

IOS之导航控制器的更多相关文章
- iOS:导航控制器侧滑出栈实现
介绍:在iOS中,导航控制器UINavigationController是默认实现左侧边缘侧滑手势出栈的,但是如果当开发者对导航控制器子控制实现自定义leftBaButtonItem时,这个侧滑功能就 ...
- IOS UINavigationController 导航控制器
/** 导航控制器掌握: 1.创建导航控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootVie ...
- IOS中导航控制器的代理及隐藏控制器刚出现时的滚动条
一.导航控制器的代理 1.UINavigationController的delegate属性 2.代理方法 1> 即将显示新控制器时调用 /* navigationController : 导航 ...
- iOS结合导航控制器和标签栏控制器
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name=& ...
- iOS 隔离导航控制器
题外话:最近这两个月一直很闲,项目上基本没有啥大的需求.对于程序员来说,如果没有需求其实是一件很难受的事情,之前好多次在项目中没事找事,该优化的优化,该整理的整理.可能好多程序员都遇到过与我类似的情况 ...
- IOS之导航控制器传值
UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped.这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显 ...
- iOS开发UINavigation——导航控制器UINavigationController
iOS开发UINavigation系列一——导航栏UINavigtionBar摘要iOS中的导航条可以附着于导航控制器之中使用,也可以在controller中单独使用,这篇博客,主要讨论有关导航栏的使 ...
- iOS开发UI篇—多控制器和导航控制器简单介绍
iOS开发UI篇—多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...
- IOS开发UI篇—导航控制器属性和基本使用
IOS开发UI篇—导航控制器属性和基本使用 一.导航控制器的一些属性和基本使用 1.把子控制器添加到导航控制器中的四种方法 (1) 1.创建一个导航控制器 UINavigationController ...
随机推荐
- nc工具学习
0x00.命令详解 基本使用 想要连接到某处:nc [-options] ip port 绑定端口等待连接:nc -l -p port ip 参数: -e prog 程序重定向,一旦连接,就执行 [ ...
- 解决Ubuntu 16.04下提示boot分区空间不足的办法
原文地址: http://www.jb51.net/article/106976.htm https://www.linuxidc.com/Linux/2015-09/123227.htm 因为lin ...
- [CEOI2008]order --- 最小割
[CEOI2008]order 题目描述: 有N个任务,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完成. 现在给出这些参数, ...
- 谁是最快的Go Web框架
根据Julien Schmidt测试框架中测试到的go web框架,在加上lion,fasthttp,一共测试了下面的web框架. default http macaron go-json-rest ...
- hdu 4676 Sum Of Gcd 莫队+phi反演
Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...
- UVALive 5971
Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it ...
- nginx 注册为linux系统服务
#! /bin/sh # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve ...
- centos配置虚拟主机virtualhost,让服务器支持多网站多域名(转)
如何让centos(redhat)配置虚拟主机,让服务器支持多个网站,针对Apache,只需要你修改apache配置文件/etc/httpd/conf/httpd.conf即可. 里面有个exampl ...
- perf 移植
perf 移植 perf工具用于系统性能的调优,程序优化.源码在kenel/tools/perf目录. 我在imx6平台上进行移植.将自己的移植过程记录如下. 参考链接 http://blog.csd ...
- 获取WINDOWS打印机列表
获取WINDOWS打印机列表 如何知道WINDOWS已经安装了哪些打印机? 1) usesVcl.Printers 2) Printer.Printers // property Printers: ...