1、UINavigationController导航控制器如何使用

UINavigationController可以翻译为导航控制器,在IOS里经常用到。

我们看看它的如何使用:

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

上图来自苹果官网。

2、UINavigationController的结构组成

看下图,UINavigationController有Navigation bar  ,Navigation View ,Navigation toobar等组成。

现在我们建立一个例子,看看如何使用UINavigationController

3、新建一个项目

命名为UINavigationControllerDemo,为了更好理解UINavigationController,我们选择Empty Application模板

4、创建一个View Controller,命名为RootViewController:依次选择File——New——New File,默认勾上With XIB for user interface.

选择正确位置创建完成,这时项目里多了三个文件,分别是RootViewController.h RootViewController.m RootViewController.xib文件。

打开RootViewController.xib,添加一个按钮控件,按钮Button改成 :Goto SecondView,为跳转做准备

5、打开AppDelegate.h,向其中添加属性:

  1. @property (strong, nonatomic) UINavigationController *navController;

添加后AppDelegate.h文件代码如下:

  1. #import <UIKit/UIKit.h>
  2. @class ViewController;
  3. @interface AppDelegate : UIResponder <UIApplicationDelegate>
  4. @property (strong, nonatomic) UIWindow *window;
  5. @property (strong, nonatomic) ViewController *viewController;
  6. @property (strong, nonatomic) UINavigationController *navController;
  7. @end

6、在AppDelegate.m 文件的didFinishLaunchingWithOptions方法中创建添加navController,RootViewController视图。

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  4. RootViewController *rootView = [[RootViewController alloc] init];
  5. rootView.title = @"Root View";
  6. self.navController = [[UINavigationController alloc] init];
  7. [self.navController pushViewController:rootView animated:YES];
  8. [self.window addSubview:self.navController.view];
  9. [self.window makeKeyAndVisible];
  10. return YES;
  11. }

给rootView的titie命名为 Root View,好识别View直接的切换关系。用pushViewController把rootView加入到navController的视图栈中。

7、现在Root视图添加完成

看看效果:

'

现在还没有Navigation bar 。只有title。

8、添加UIBarButtonItem

bar ButtonItem分左右UIBarButtonItem。我们把左右的都添加上去。

在RootViewController.m中添加代码如下:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];
  5. self.navigationItem.leftBarButtonItem = leftButton;
  6. UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd  target:self action:@selector(selectRightAction:)];
  7. self.navigationItem.rightBarButtonItem = rightButton;<p class="p1">}</p>

这样添加了UIBarButtonItem了,效果如下:

这里重点介绍下

UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(selectLeftAction:)];

UIBarButtonSystemItemAction的风格,这是系统自带的按钮风格,看下图,你不用一个个试验,你也知道想用那个item,如下图:

9、响应UIBarButtonItem的事件的实现

我们在 action:@selector(selectLeftAction:);

action添加了selectLeftAction和selectRightAction

在RootViewController.m文件中添加代码实现:

  1. -(void)selectLeftAction:(id)sender
  2. {
  3. UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏左按钮" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  4. [alter show];
  5. }
  6. -(void)selectRightAction:(id)sender
  7. {
  8. UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏右按钮" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  9. [alter show];
  10. }

这样在点击左右的UIBarButtonItem时,弹出提示:

这篇先讲添加UIBarButtonItem,下篇讲解页面跳转和添加UISegmentedControl

例子代码:https://github.com/schelling/YcDemo

本文转载至:http://my.oschina.net/jackyyang/blog/67151

UIBarButtonItem的更多相关文章

  1. 自定义UIBarButtonItem

    如果是通过UIButton自定义UIBarButtonItem,那么通过如下这个方式设置title是无效的.必须要直接给button设置title. self.navigationItem.right ...

  2. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

  3. UIBarButtonItem的创建

    1. rightBarButtonItem的创建 -(void)initRightBar{ UIBarButtonItem *done =[UIBarButtonItem rightItemWithT ...

  4. 新浪微博客户端(3)-封装UIBarButtonItem

    单独给NavigationBar上的两个NavigationItem设置图片显得比较麻烦,下面对创建单个UIBarButtonItem的过程进行封装. UIBarButtonItem+Extensio ...

  5. UIBarButtonItem不能获取frame

    在使用KxMenu这个厉害的控件做竖直列表的时候,发现UIBarButtonItem不能获取到frame,UIBarButtonItem是NSObject的子类,他不是一个uiresponed或者ui ...

  6. UIBarButtonItem变弹簧

    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystem ...

  7. UINavigationController详解一(转)UIBarButtonItem

    本文出自:http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html 特别感谢. 1.UINavigationControlle ...

  8. [转]iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    转载地址:http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINav ...

  9. ios基础篇(十)——UINavgationController的使用(一)UIBarButtonItem的添加

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

  10. ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正

    为UINavigationBar 写一个分类.UINavigationItem+correct_offset.h  转载 http://www.colabug.com/thread-1112420-1 ...

随机推荐

  1. 基于HTML5/CSS3图片网格动画特效

    现在HTML5技术可以让网页上的图片变得非常神奇,各种各样的HTML5图片动画特效让你眼花缭乱.今天要分享的这款HTML5图片网格动画特效就非常炫酷.图片缩略图按网格的布局一行行排列,你只需点击按钮即 ...

  2. 实现qsort(和qsort差一个数量级啊,伤自尊了)

    #include <cstdio> #include <cstdint> #include <ctime> #include <cstring> #in ...

  3. C语言 · 整除问题

    算法训练 整除问题   时间限制:1.0s   内存限制:512.0MB      问题描述 编写一个程序,输入三个正整数min.max和factor,然后对于min到max之间的每一个整数(包括mi ...

  4. buildroot 文件系统添加telnet, ssh, 以及制作注意事项

    buildroot 制作Linux嵌入式文件系统,并添加telnet 以及ssh * sshd 服务的添加 // make menuconfig Target options ---> Targ ...

  5. [uart]2.tty和uart的函数调用流程

    以下是在include/uapi/linux/tty.h中定义了现有的线规号,如果需要定义新的,则需要在后面添加新的 /* line disciplines */ #define N_TTY 0 #d ...

  6. 对设计领域中Tile和Card的理解

    前端工程师离不开设计, 谈到设计就要想到大名鼎鼎的material design主题, 而material是以card为经典单元的, card即卡片, 是层次化模型的最小模块, 用于提供扁平化的信息, ...

  7. mysql客户端(Navicat)远程登录操作遇到问题1142 - XXX command denied to ×××

    新建的dbtest用户,访问新建立的TestDB数据库每次都提示1142错误(已经对数据库TestDB赋予了所有权限) 解决方法: Mysql命令: GRANT SELECT ON mysql.* T ...

  8. css样式DEMO

    <!-- 导入框 --> <div id="importWin" class="easyui-window" title="服务封装 ...

  9. tensorflow函数学习笔记

    https://www.w3cschool.cn/tensorflow_python/tensorflow_python-4isv2ez3.html tf.trainable_variables返回的 ...

  10. 【转】 如何利用C#代码来进行操作AD

    要用代码访问 Active Directory域服务,需引用System.DirectoryServices命名空间,该命名空间包含两个组件类,DirectoryEntry和 DirectorySea ...