【iOS UI】UINavigationController
1.1简介
UINavigationController可以翻译为导航控制器,在iOS里经常用到。
下面的图显示了导航控制器的流程。最左侧是根视图,当用户点击其中的General项时 ,General视图会滑入屏幕;当用户继续点击Auto-Lock项时,Auto-Lock视图将滑入屏幕。相应地,在对象管理上,导航控制器使用了导航堆栈。根视图控制器在堆栈最底层,接下来入栈的是General视图控制器和Auto-Lock视图控制器。可以调用pushViewControllerAnimated:方法将视图控制器推入栈顶,也可以调用popViewControllerAnimated:方法将视图控制器弹出堆栈。

1.2UINavigationController结构组成
看下图,UINavigationController有Navigation bar ,Navigation View ,Navigation toolbar等组成。

2.1
- 新建一个空项目UINavigationControllerDemo
- 新建一个UIViewController,并在UIViewController.xib中添加一个Button设置名字为Goto SecondView
- 打开AppDelegate.h,添加属性
#import <UIKit/UIKit.h> @interface MLKAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong,nonatomic) UINavigationController *navController; @end
AppDelegate.mdidFinishLaunchingWithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MLKRootViewController *rootController=[[MLKRootViewController alloc]init];
rootController.title=@"Root View"; self.navController=[[UINavigationController alloc]init];
[self.navController pushViewController:rootController animated:YES];
//
[self.window addSubview:self.navController.view];
//
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
运行

2.2 添加UIBarButtonItem
bar ButtonItem分左右UIBarButtonItem。我们把左右的都添加上去。
在RootViewController.m中添加代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//添加UIBarButtonItem
UIBarButtonItem *leftButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];
//
self.navigationItem.leftBarButtonItem=leftButton; UIBarButtonItem *rightButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectRightAction:)];
//
self.navigationItem.rightBarButtonItem=rightButton;
}
UIBarButtonSystemItemAction的风格,这是系统自带的按钮风格,系统自带的按钮有下面这些

2.3响应UIBarButtonItem的点击事件
//响应UIBarButtonItem事件的实现
-(void)selectLeftAction:(id)sender{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航左按钮" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show];
} -(void)selectRightAction:(id)sender{ UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航右按钮" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; }

3.1创建一个新的UIViewController SecondViewController
3.2为RootViewController的Button设置点击方法
-(void)goToSecondView:(id)sender{
MLKSecondViewController *secondView=[[MLKSecondViewController alloc]init];
[self.navigationController pushViewController:secondView animated:YES];
secondView.title=@"Second View";
}
SecondViewController页面

如何在导航栏中实现这种效果呢

这就是SegmentedControl
在SecondViewController.m的viewDidLoad添加如下代码
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSArray *array = [NSArray arrayWithObjects:@"鸡翅",@"排骨", nil];
UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array]; segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter; [segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentedController; }
设置点击事件
-(void)segmentAction:(id)sender
{
switch ([sender selectedSegmentIndex]) {
case :
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了鸡翅" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show]; }
break;
case :
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了排骨" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
}
break; default:
break;
}
}

左上角的返回上级View的barButtonitem的名字是上级目录的Title,如果title或者适合做button的名字,怎么办呢?我们可以自己定义
在RootViewController viewDidLoad方法
//自定义backBarButtonItem
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem=backButton;

5.1显示ToolBar
在SecondViewController的viewDidLoad方法中添加下面的代码这样ToolBar就显示出来了
[self.navigationController setToolbarHidden:NO animated:YES];
在ToolBar上添加UIBarButtonItem
UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];
UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];

注意:用 [self.navigationController.toolbar setItems:(NSArray *) animated:<#(BOOL)#>]这个方法添加item是不起效果的。下面我动态自己添加Toolbar时,这个才起效果。
5.2动态添加ToolBar
SecondViewController.h文件中添加属性
@property UIToolbar *toolBar;
//先隐藏ToolBar
[self.navigationController setToolbarHidden:YES animated:YES];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(goToThirdView:)];
self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - _toolBar.frame.size.height - 44.0, self.view.frame.size.width, 44.0)];
[_toolBar setBarStyle:UIBarStyleDefault];
_toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[_toolBar setItems:[NSArray arrayWithObject:searchButton]];
[self.view addSubview:_toolBar];
响应UIBarButtonItem的点击事件,跳转到第三个页面去
-(void)goToThirdView:(id)sender{
MLKThirdViewController *thirdView=[[MLKThirdViewController alloc]init];
[self.navigationController pushViewController:thirdView animated:YES];
thirdView.title=@"Third View";
}
【iOS UI】UINavigationController的更多相关文章
- 【iOS系列】-UINavigationController的使用(Segue传递数据)
[iOS系列]-UINavigationController的使用 UINavigationController是以以栈(先进后出)的形式保存子控制器, 常用属性: UINavigationItem有 ...
- 【iOS系列】-xib封装使用
[iOS系列]-xib封装使用 Xib文件可以用来描述某一块局部的UI界面 Xib文件的加载 修改xib文件的大小size(Freeform) 第一: NSArray *objs = [[NSBund ...
- 【iOS系列】-iOS的多线程解析
[iOS系列]-iOS的多线程解析 iOS的多线程实现技术: 1:GCD -- Grand Central Dispatch 是基于C语言的底层API 用Block定义任务,使用起来非常灵活便捷 提供 ...
- 【转】【iOS开发】打开另一个APP(URL Scheme与openURL)
目标 平常我们做iOS开发,会经常遇到打开其他的APP的功能.本篇文章讲的就是打开别人的APP的一些知识.我们的目标是: 打开别人的APP 让别人打开我们的APP iOS9的适配问题 使用URL Sc ...
- 【iOS系列】-自定义Modar动画
[iOS系列]-自定义Modar动画.md 我们需要做的最终的modar动画的效果是这样的, 就是点击cell,cell发生位移,慢慢的到第二个界面上的.为了做出这样的动画效果,我们需要以下的知识. ...
- 【iOS系列】- iOS吸附效果的实现 之 UICollectionView的使用全解
[iOS系列]- iOS吸附效果的实现 之 UICollectionView的使用全解 UICollectionView可以做很多的布局,在iOS开发中较为重要,所以这里就以实例来讲解UICollec ...
- 【iOS系列】-A server with the specified hostname could not be found.问题解决
[iOS系列]-A server with the specified hostname could not be found.问题解决 Reveal 在iOS开发中可以方便查看界面的布局,较为方便的 ...
- 【iOS系列】-iOS查看沙盒文件图文教程(真机+模拟器)
[iOS系列]-iOS查看沙盒文件图文教程(真机+模拟器) 1:模拟器 1.1 方法1: 程序中打印一下的地址,能直接前往沙盒路径. NSString *path = [NSSearchPathFor ...
- 【iOS系列】-UIWebView加载网页禁止左右滑动
[iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...
随机推荐
- 浅谈!SQL语句中LEFT JOIN ON WHERE和LEFT JOIN ON AND的区别
今天的工作学习之路是一个数据库的小知识,当时没有区分出所以然,特此记录分享一下子. 众所周知,数据库的表都是单独存在的,但是当我们进行联合查询(多表查询)时,我们获得数据库返回的值时就好像在一张表里一 ...
- JVM中堆内存和栈内存的区别
Java把内存分成两种,一种叫做栈内存,一种叫做堆内存 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,java就在栈中为这个变量分配内存空间 ...
- Beautils工具类实现的原理
关于内省机制和反射机制请看这一篇博客[还没写完,在草稿中]. 先说一下什么叫做 bean 属性,bean 属性指的是 get / set 方法后的名称,而不是类的属性: 比如: private Str ...
- supervisor安装配置
1.安装 下载:https://codeload.github.com/Supervisor/supervisor/zip/3.1.3 2.安装 .zip cd supervisor- python ...
- python 之tornado 入门
#!/usr/bin/env python # -*- coding:utf-8 -*- # --------------------------------------- # email : gen ...
- shiro使用教程
一.shiro是什么 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.不仅可以在Web项目中使用,在普通的项目中也是可以使用的 二.shiro可以做什 ...
- 记一次synchronized锁字符串引发的坑兼再谈Java字符串
问题描述 业务有一个需求,我把问题描述一下: 通过代理IP访问国外某网站N,每个IP对应一个固定的网站N的COOKIE,COOKIE有失效时间.并发下,取IP是有一定策略的,取到IP之后拿IP对应的C ...
- Async/Await替代Promise的6个理由
译者按: Node.js的异步编程方式有效提高了应用性能:然而回调地狱却让人望而生畏,Promise让我们告别回调函数,写出更优雅的异步代码:在实践过程中,却发现Promise并不完美:技术进步是无止 ...
- poptest老李谈jvm的GC
poptest老李谈jvm的GC poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:90882 ...
- Web性能优化工具WebPageTest(一)——总览与配置
网站性能优化工具大致分为两类:综合类和RUM类(实时监控用户类),WebPageTest属于综合类. WebPageTest通过布置一些特定的场景进行测试,例如不同的网速.浏览器.位置等. 测试完成后 ...