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之导航控制器的更多相关文章

  1. iOS:导航控制器侧滑出栈实现

    介绍:在iOS中,导航控制器UINavigationController是默认实现左侧边缘侧滑手势出栈的,但是如果当开发者对导航控制器子控制实现自定义leftBaButtonItem时,这个侧滑功能就 ...

  2. IOS UINavigationController 导航控制器

    /** 导航控制器掌握: 1.创建导航控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootVie ...

  3. IOS中导航控制器的代理及隐藏控制器刚出现时的滚动条

    一.导航控制器的代理 1.UINavigationController的delegate属性 2.代理方法 1> 即将显示新控制器时调用 /* navigationController : 导航 ...

  4. iOS结合导航控制器和标签栏控制器

    <span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name=& ...

  5. iOS 隔离导航控制器

    题外话:最近这两个月一直很闲,项目上基本没有啥大的需求.对于程序员来说,如果没有需求其实是一件很难受的事情,之前好多次在项目中没事找事,该优化的优化,该整理的整理.可能好多程序员都遇到过与我类似的情况 ...

  6. IOS之导航控制器传值

    UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped.这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显 ...

  7. iOS开发UINavigation——导航控制器UINavigationController

    iOS开发UINavigation系列一——导航栏UINavigtionBar摘要iOS中的导航条可以附着于导航控制器之中使用,也可以在controller中单独使用,这篇博客,主要讨论有关导航栏的使 ...

  8. iOS开发UI篇—多控制器和导航控制器简单介绍

    iOS开发UI篇—多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...

  9. IOS开发UI篇—导航控制器属性和基本使用

    IOS开发UI篇—导航控制器属性和基本使用 一.导航控制器的一些属性和基本使用 1.把子控制器添加到导航控制器中的四种方法 (1) 1.创建一个导航控制器 UINavigationController ...

随机推荐

  1. jdk1.8下字符串常量的判断,String.intern()分析

    字符串常量池在jdk升级过程中发生了一些变化 在JDK1.6中,它在方法区中,属于“永久代”. 在JDK1.7中,它被移除方法区,放在java堆中. 在JDK1.8中,取消了“永久代”,将常量池放在元 ...

  2. 美团外卖Android平台化的复用实践

    美团外卖平台化复用主要是指多端代码复用,正如美团外卖iOS多端复用的推动.支撑与思考文章所述,多端包含有两层意思:其一是相同业务的多入口,指美团外卖业务需要在美团外卖App(下文简称外卖App)和美团 ...

  3. 在windows安装配置Git开发环境

    开始配置Git的开发环境.首先从google  code下载最新的windows的git安装包msysgit,当时我下载的是Git-1.7.4-preview20110204.exe,然后就开始安装了 ...

  4. 使用 Nokia Imaging SDK 开发有滤镜功能的 Windows Phone 8 应用

    说到滤镜应用,相信很多数开发者都对照片特效的经验都十分有限,通常都是去找一些三方的类库进行学习或移植,今天在这里给大家介绍下 Nokia 的 Imaging SDK, 相信大家对Nokia的自家图像软 ...

  5. Codeforces Round #279 (Div. 2) B - Queue 水题

    #include<iostream> #include<mem.h> using namespace std; ],q[]; int main() { int n,x,y; m ...

  6. ServletActionContext.getRequest().getSession() 和 ActionContext.getContext().getSession()

    ActionContext.getContext().getSession(); 这个方法获取的session是struts封装过的一个Map类型的session,只能调用put()方法缓存数据. S ...

  7. mysql 3.2.49 源代码安装-redhat 5 x64

    [mysql@localhost ~]$ uname -r2.6.32 [root@localhost ~]#cp /usr/include/pthread.h /usr/include/pthrea ...

  8. [Asp.net MVC]HandleErrorAttribute异常过滤器

    摘要 在asp.net mvc中除了使用try...catch/finally来处理异常外,它提供了一种通过在Controller或者Action上添加特性的方式来处理异常. HandleErrorA ...

  9. C#程序集系列06,程序集清单,EXE和DLL的区别

    CLR在加载程序集的时候会查看程序集清单,程序集清单包含哪些内容呢?可执行文件和程序集有什么区别/ 程序集清单 □ 查看程序集清单 →清空F盘as文件夹中的所有内容→创建MainClass.cs文件→ ...

  10. 使用Bootstrap 3开发响应式网站实践01,前期准备、导航区域等

    "使用Bootstrap 3开发响应式网站实践"系列,将使用Bootstrap 3.2制作一个自适应网站,无论是在电脑.平板,还是手机上,都呈现比较好的效果.在电脑浏览器上的最终效 ...