【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加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...
随机推荐
- python如何保证输入键入数字
要求:python写一个要求用户输入数字,如果不是数字就一直循环要求输入,直到输入数字为止的代码 错误打开方式: while True: ten=input('Enter a number:') if ...
- ROS多机通信计算机网络配置
以实现master和nico的互联共享信息为例 1 查看IP地址 $ifconfig 查看ip地址 可以看到 master的IP为192.168.1.10 nico的IP为192.168.1.103 ...
- [SinGuLaRiTy] 树形DP专项测试
[SinGuLaRiTy-1015] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 对于所有的题目:Time Limit:1s | Me ...
- 老李分享:robotium3.6与4.0 later 的区别 1
老李分享:robotium3.6与4.0 later 的区别 因为下载的直接是最新版本的robotium4.1版,这次碰到gridView问题时,发现网上有getCurrentListViews( ...
- Xmpp实现简单聊天系列 --- ①openfire部署
1. 下载最新的openfire安装文件 官方下载站点:http://www.igniterealtime.org/downloads/index.jsp#openfire 2. 下载完成后,执行你的 ...
- Android 开发之错误整理 [2014-04-28 09:22:28 - XXXX] Unable to resolve target 'android-18'
在开发的时候难免会导入项目,那么怎么经常会遇到这个错误: [2014-04-28 09:22:28 - XXXX] Unable to resolve target 'android-18' targ ...
- SpringMVC基础学习(一)—初识SpringMVC
一.HelloWorld 1.导入SpringMVC所需的jar包 2.配置web.xml 配置DispatcherServlet.DispatcherServlet默认加载/ ...
- mysql 分析5语句的优化--索引添加删除
查看表的索引 show create table 表名; show index from 表名; show keys from表名; 添加索引 alter table 表名 add index 索 ...
- JavaScript运算符优先级
图片引用自:http://www.cnblogs.com/xxcanghai/p/5189353.html
- iOS各框架功能简述以及系统层次结构简单分析
iOS各个框架所对应的功能简单介绍 iOS系统结构层次: