1.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.UINavigationController简单使用

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.UINavigationController实现页面切换

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页面

4.在导航栏中使用SegmentedControl

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

这就是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.ToolBar

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的更多相关文章

  1. 【iOS系列】-UINavigationController的使用(Segue传递数据)

    [iOS系列]-UINavigationController的使用 UINavigationController是以以栈(先进后出)的形式保存子控制器, 常用属性: UINavigationItem有 ...

  2. 【iOS系列】-xib封装使用

    [iOS系列]-xib封装使用 Xib文件可以用来描述某一块局部的UI界面 Xib文件的加载 修改xib文件的大小size(Freeform) 第一: NSArray *objs = [[NSBund ...

  3. 【iOS系列】-iOS的多线程解析

    [iOS系列]-iOS的多线程解析 iOS的多线程实现技术: 1:GCD -- Grand Central Dispatch 是基于C语言的底层API 用Block定义任务,使用起来非常灵活便捷 提供 ...

  4. 【转】【iOS开发】打开另一个APP(URL Scheme与openURL)

    目标 平常我们做iOS开发,会经常遇到打开其他的APP的功能.本篇文章讲的就是打开别人的APP的一些知识.我们的目标是: 打开别人的APP 让别人打开我们的APP iOS9的适配问题 使用URL Sc ...

  5. 【iOS系列】-自定义Modar动画

    [iOS系列]-自定义Modar动画.md 我们需要做的最终的modar动画的效果是这样的, 就是点击cell,cell发生位移,慢慢的到第二个界面上的.为了做出这样的动画效果,我们需要以下的知识. ...

  6. 【iOS系列】- iOS吸附效果的实现 之 UICollectionView的使用全解

    [iOS系列]- iOS吸附效果的实现 之 UICollectionView的使用全解 UICollectionView可以做很多的布局,在iOS开发中较为重要,所以这里就以实例来讲解UICollec ...

  7. 【iOS系列】-A server with the specified hostname could not be found.问题解决

    [iOS系列]-A server with the specified hostname could not be found.问题解决 Reveal 在iOS开发中可以方便查看界面的布局,较为方便的 ...

  8. 【iOS系列】-iOS查看沙盒文件图文教程(真机+模拟器)

    [iOS系列]-iOS查看沙盒文件图文教程(真机+模拟器) 1:模拟器 1.1 方法1: 程序中打印一下的地址,能直接前往沙盒路径. NSString *path = [NSSearchPathFor ...

  9. 【iOS系列】-UIWebView加载网页禁止左右滑动

    [iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...

随机推荐

  1. 关于php的flush在本机正常在服务器不灵的问题

    这个问题网上很多,我就不重复那些了. 我的是关于进度条的应用.我最后遇到的问题是,在本机swampserver环境下的输出缓存很快,但是到了服务器上就是一段一段的了.我的服务器是Web服务器是IIS. ...

  2. select count(*)优化 快速得到总记录数

    1.select count(*) from table_name 比select count(主键列) from table_name和select count(1) from table_name ...

  3. 常见端口、端口查询及TCP状态

    查看电脑端口的开放情况命令:cmd——netstat -a -n -a:显示所有连接和监听端口:-n:以数字形式显示地址和端口号 “本地地址”指本地IP地址及其正在使用的端口号,“外部地址”指连接某端 ...

  4. iOS开发之使程序在后台运行

    方法一(此方法不太可靠): 开启程序后台运行: [application beginBackgroundTaskWithExpirationHandler:^{ //后台运行过期后会调用此block内 ...

  5. spring(一) IOC讲解

    spring基本就两个核心内容,IOC和AOP.把这两个学会了基本上就会用了. --WH 一.什么是IOC? IOC:控制反转,通俗点讲,将对象的创建权交给spring,我们需要new对象,则由spr ...

  6. 解决Appium 抓取toast

    首先我们先看看这个gif,图中需要,要抓取的字符串--->请输入转让份数 1.要导入java-client-5.0.0-SNAPSHOT.jar 包的地址:链接:http://pan.baidu ...

  7. cephfs创建及挂载

    Ceph 文件系统( Ceph FS )是个 POSIX 兼容的文件系统,它使用 Ceph 存储集群来存储数据.Ceph 文件系统要求 Ceph 存储集群内至少有一个 Ceph 元数据服务器. 1.添 ...

  8. centOS下调整swap

    [root@localhost /]# mkdir swap [root@localhost /]# cd swap [root@localhost swap]# dd if=/dev/zero of ...

  9. as3中textField输入字符时,一次性过长后自动换行

    txt_show.text = showStr; var str:String = txt_show.text; var strlen:int = str.length; var len:int = ...

  10. Linux上常用的文件传输方式以及比较

    tp ftp 命令使用文件传输协议(File Transfer Protocol, FTP)在本地主机和远程主机之间或者在两个远程主机之间进行文件传输. FTP 协议允许数据在不同文件系统的主机之间传 ...