UINavigationController又被成为导航控制器,继承自UIViewController,以栈的方式管理所控制的视图控制器,下面就详细说一下UINavigationController的使用:

1、首先新建一个工程(就不多说了)创建RootViewController(继承自UIViewController)。

2、打开AppDelegate.h文件添加属性

3、打开AppDelegate.m文件的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法添加navController和根视图(RootViewController)。

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. // Override point for customization after application launch.
  3.  
  4. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  5. RootViewController *rootVC = [[RootViewController alloc] init];
  6. rootVC.title = @"RootViewController";
  7.  
  8. //初始化navController
  9. self.navController = [[UINavigationController alloc] init];
  10. //给rootVC添加推入动作
  11. [self.navController pushViewController:rootVC animated:YES];
  12. //将navController加到window上
  13. [self.window addSubview:self.navController.view];
  14.  
  15. [self.window makeKeyAndVisible];
  16.  
  17. return YES;
  18. }

效果图:

4、添加UIBarButtonItem

注:UIBarButtonItem分为leftBarButtonItem和rightBarButtonItem;

  1. #import "RootViewController.h"
  2.  
  3. @interface RootViewController (){
  4.  
  5. UIBarButtonItem *leftButton;
  6. UIBarButtonItem *rightButton;
  7.  
  8. }
  9.  
  10. @end
  11.  
  12. @implementation RootViewController
  13.  
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. // Do any additional setup after loading the view from its nib.
  17.  
  18. leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(leftButtonAction:)];
  19. self.navigationItem.leftBarButtonItem = leftButton;
  20.  
  21. rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(rightButtonAction:)];
  22. self.navigationItem.rightBarButtonItem = rightButton;
  23.  
  24. }

效果图:

这里还要说一下initWithBarButtonSystemItem:即系统自带按钮风格:

UIBarButtonSystemItemDone:蓝色文字按钮,标有“Done”;

UIBarButtonSystemItemCancel:文字按钮,标有“Cancel”;

UIBarButtonSystemItemEdit:文字按钮,标有“Edit”;

UIBarButtonSystemItemSave:蓝色文字按钮,标有“Save”;

UIBarButtonSystemItemAdd:图像按钮,上面有一个Å符号;

UIBarButtonSystemItemFlexibleSpace:空白,占用空间大小可变;

UIBarButtonSystemItemFixedSpace:空白占位符;

UIBarButtonSystemItemCompose:图像按钮,上有一支笔和纸张;

UIBarButtonSystemItemReply:图像按钮,上有一个回复箭头;

UIBarButtonSystemItemAction:图像按钮,上有一个动作箭头;

UIBarButtonSystemItemOrganize:图像按钮,上有一个文件夹以及向下箭头;

UIBarButtonSystemItemBookmarks:图像按钮,上有书签图标;

UIBarButtonSystemItemSearch:图像按钮,上有spotlight图标;

UIBarButtonSystemItemRefresh:图像按钮,上有一个环形的刷新箭头;

UIBarButtonSystemItemStop:图像按钮,上有一个停止记号X;

UIBarButtonSystemItemCamera:图像按钮,上有一个照相机;

UIBarButtonSystemItemTrash:图像按钮,上有一个垃圾桶;

UIBarButtonSystemItemPlay:图像按钮,上有一个播放图标;

UIBarButtonSystemItemPause:图像按钮,上有一个暂停图标;

UIBarButtonSystemItemRewind:图像按钮,上有一个倒带图标;

UIBarButtonSystemItemFastForward:图像按钮,上有一个快进图标;

5、UIBarButtonItem的事件的实现

  1. - (void)leftButtonAction:(UIBarButtonItem*)leftAction{
  2.  
  3. UIAlertView *leftAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否继续编辑" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
  4. [leftAlert show];
  5. }
  6.  
  7. - (void)rightButtonAction:(UIBarButtonItem*)rightAction{
  8.  
  9. UIAlertView *rightAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"退出" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
  10. [rightAlert show];
  11.  
  12. }

效果图:

ios基础篇(十)——UINavgationController的使用(一)UIBarButtonItem的添加的更多相关文章

  1. ios基础篇(十二)——UINavgationController的使用(三)ToolBar

    UIToolBar存在于UINavigationController导航栏控制器中,而且默认被隐藏:设置UINavigationController的toolbarHidden属性可显示UIToolB ...

  2. ios基础篇(十六)——UIWebView的基本使用

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档等.UIWebView是一个混合体,具体的功能控件内置的,实现一些基本的功能.UIWebView可以查看Html网页,pdf文件,do ...

  3. ios基础篇(二十六)—— UITableViewCell的分组索引与标记

    一.表视图的索引目录 首先要创建一个TableView,之前有说过,这里就不详细说了(参考前面第十四篇). 直接贴代码吧, #import "ViewController.h" @ ...

  4. ios基础篇(二十九)—— 多线程(Thread、Cocoa operations和GCD)

    一.进程与线程 1.进程 进程是指在系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内: 如果我们把CPU比作一个工厂,那么进程就好比工厂的车间,一个工厂有 ...

  5. ios基础篇(二十五)—— Animation动画(UIView、CoreAnimation)

    Animation主要分为两类: 1.UIView属性动画 2.CoreAnimation动画 一.UIView属性动画 UIKit直接将动画集成到UIView类中,实现简单动画的创建过程.UIVie ...

  6. ios基础篇(二十四)—— 文字、图片的绘制及其自定义Button

    这篇文章我们主要来拿官方的控件来研究一下,我们来仿照官方的控件,自己来实现它提供的控件: 首先来看看基本的图片与文字的绘制,很简单. 一.imageView 所有的视图都是继承自UIView,所以我们 ...

  7. ioS基础篇(十九)——UIResponder简析

    UIResponder类定义了对象相应和控制事件的接口,他是UIApplication.UIView的超类,这类的实例通常被称为应答对象. 一.Responder对象 在iOS系统中,能够响应并处理事 ...

  8. ios基础篇(十八)——Delegate 、NSNotification 和 KVO用法及其区别

    一.Delegate Delegate本质是一种程序设计模型,iOS中使用Delegate主要用于两个页面之间的数据传递.iphone中常用@protocol和delegate的机制来实现接口的功能. ...

  9. iOS基础篇(十五)——UIScrollView的基本用法

    滚动视图(UIScrollView)通常用于显示内容尺寸大于屏幕尺寸的视图. 一.基本属性 1.CGSize contentSize :设置UIScrollView的滚动范围 2.CGPoint co ...

随机推荐

  1. GATK3.2.2小结(转载)

    http://blog.csdn.net/skenoy/article/details/38346489 经过几天的摸索和网上资料的查询对GATK软件有点小心得,现总结如下: 1. fasta文件最好 ...

  2. Nginx简介及配置实用

    Nginx简介 Nginx是一个高性能的HTTP和反向代理服务器: 支持的操作系统众多,windows.linux. MacOS X: 可实现负载均衡: Rewrite功能强大: 电商架构大部分都采用 ...

  3. ACDC

    acdc dcdc电源模块中大功率一般都是开关电源模式的,所以一般输入都是一个较宽的电源范围,体积也相对于变压器要小一些,效率高一些,但是纹波会偏大一些,如何选择就要看电路的需求来选择相应的方案

  4. 九度-剑指Offer

    二维数组中的查找 分析:既然已经给定了每一行从左至右递增,那么对于每一行直接二分查找即可,一开始还想着每一列同样查找一次,后来发现每一行查找一遍就能够遍历所有的元素了. #include <cs ...

  5. iOS - UIViewController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIViewController : UIResponder <NSCoding, UIAppearanceC ...

  6. apt-get下载的文件

    1. http://kurenai.elastos.org/2013/05/02/ubuntu-apt-get%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86/ http:// ...

  7. [转载] 理解 epoll 的事件触发机制

    原文: http://weibo.com/p/1001603862394207076573?sudaref=weibo.com epoll的I/O事件触发方式有两种模式:ET(Edge Trigger ...

  8. (一)解决Sublime Text 2中文显示乱码问题

    欲解决问题,关键在于让Sublime Text 2支持GB2312和GBK.步骤如下: 1.安装Sublime Package Control.   在Sublime Text 2上用Ctrl+-打开 ...

  9. linux GO语言配置安装

    1.下载地址 https://golang.org/dl/ 2.解压 解压到/usr/local/go目录下 cd /usr/local/go bin/go version 执行如上命令出现go的版本 ...

  10. 闲谈--心态 (zhuan)

    http://blog.csdn.net/marksinoberg/article/details/53261034 ***************************************** ...