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

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

下篇:iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

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

UINavigationController详解一(转)UIBarButtonItem的更多相关文章

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

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

  2. [转]iOS学习之UINavigationController详解与使用(三)ToolBar

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...

  3. [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...

  4. iOS学习之UINavigationController详解与使用(三)ToolBar

    1.显示Toolbar  在RootViewController.m的- (void)viewDidLoad方法中添加代码,这样Toobar就显示出来了. [cpp] view plaincopy [ ...

  5. iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

    iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem是上篇,我们接着讲UINavigationController的重要作用,页面的管理和切换. ...

  6. UI第六节——UINavigationController 详解

    1. UINavigationController 是一个容器类.里面盛放的是UIViewController. 容器的意思是,如果你不放入UIViewController,里面就是空的,什么也没有. ...

  7. IOS开发之UINavigationController详解

    UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的AP ...

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

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

  9. iOS 的UINavigationController详解与使用添加UIBarButtonItem

    转发自:http://blog.csdn.net/totogo2010/article/details/7681879 分类: iOS开发入门2012-06-21 11:10 53077人阅读 评论( ...

随机推荐

  1. cocoapods没有自动补齐

    在install cocoapods成功之后,输入头文件名的时候,没有提示,这样,我还以为是我的cocoapods安装出错了.原来是配置没有配置好. 解决办法: 选择工程的 Target -> ...

  2. php-001-win7 环境下 wamp 的至简部署

    此文主要记录学习 PHP 开发环境 wamp 的至简部署.敬请各位小主参阅,若有不足之处,敬请大神指正,不胜感激! 首先依据自身的操作系统选择,进入 wamp 的网站 :http://www.wamp ...

  3. Selenium2学习-009-WebUI自动化实战实例-007-Selenium 8种元素定位实战实例源代码(百度首页搜索录入框及登录链接)

    此 文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,通过 ID.name.xpath.cssSelector.linkText.className.partialLinkTe ...

  4. iOS Architecture和Valid architectures

    目前ios的指令集有以下几种: 1,armv6,支持的机器iPhone,iPhone2,iPhone3G及对应的iTouch 2,armv7,支持的机器iPhone4,iPhone4S 3,armv7 ...

  5. LeetCode Best Time to Buy and Sell Stock IV

    原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...

  6. 两排滚动js

    html: <div class="mr_frbox">        <div class="showtitle">          ...

  7. JQuery:JQuery添加元素

    JQuery:添加元素通过 jQuery,可以很容易地添加新元素/内容.添加新的HTML内容.我们将学习用于添加新内容的四个jQuery方法: append() - 在被选元素的结尾插入内容 prep ...

  8. ViewFlipper、ViewPager和Gallery

    1.ViewFlipper 1)View切换的控件—ViewFlipper介绍 ViewFilpper类继承于ViewAnimator类.而ViewAnimator类继承于FrameLayout. 查 ...

  9. java.net.BindException: Address already in use: JVM_Bind

    是端口占用,如果修改端口后仍提示这样的错误,可能是eclipse自动关闭或正在运行服务非正常中断导致端口没有关闭,这时你再启动,还是提示java.net.BindException: Address  ...

  10. [翻译] java NIO 教程---介绍

    原文地址:http://tutorials.jenkov.com/java-nio/index.html Java NIO(new IO)是从java1.4之后的对IO API的另一种选择,即对标准j ...