转载地址:http://blog.csdn.net/totogo2010/article/details/7682433

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

1、RootView 跳到SecondView

首先我们需要新一个View。新建SecondView,按住Command键然后按N,弹出新建页面,我们新建SecondView

2、为Button 添加点击事件,实现跳转

在RootViewController.xib中和RootViewController.h文件建立连接

在RootViewController.m中实现代码,alloc一个SecondViewController,用pushViewController到navigationController中去,并为

SecondViewController这是title为    secondView.title =@"Second View"; 默认情况下,titie为下个页面返回按钮的名字。

  1. - (IBAction)gotoSecondView:(id)sender {
  2. SecondViewController *secondView = [[SecondViewController alloc] init];
  3. [self.navigationController pushViewController:secondView animated:YES];
  4. secondView.title = @"Second View";
  5. }

这是点击GotoSecondView 按钮,出现

这就是SecondView了。

3、添加segmentedController

在nav bar这样的效果是如何实现的呢?

这就是segmentedController。

3.1在RootViewController.m的viewDidLoad添加如下代码:

  1. NSArray *array = [NSArray arrayWithObjects:@"鸡翅",@"排骨", nil];
  2. UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];
  3. segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;
  4. [segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
  5. self.navigationItem.titleView = segmentedController;

3.2[segmentedController addTarget:selfaction:的实现

  1. -(void)segmentAction:(id)sender
  2. {
  3. switch ([sender selectedSegmentIndex]) {
  4. case 0:
  5. {
  6. UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了鸡翅" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  7. [alter show];
  8. }
  9. break;
  10. case 1:
  11. {
  12. UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了排骨" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  13. [alter show];
  14. }
  15. break;
  16. default:
  17. break;
  18. }
  19. }

这样就能响应鸡翅和排骨按钮了

4、自定义backBarButtonItem

左上角的返回上级View的barButtonitem的名字是上级目录的Title,如果title或者适合做button的名字,怎么办呢?我们可以自己定义

代码如下:

  1. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根视图" style:UIBarButtonItemStyleDone target:nil action:nil];
  2. self.navigationItem.backBarBu

效果:

6、自定义title

UINavigationController的title可以用别view替代,比如用UIButton UILable等,下面我用UIButton.

在SecondViewController.m中添加下面如下。

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
  5. [button setTitle: @"自定义title" forState: UIControlStateNormal];
  6. [button sizeToFit];
  7. self.navigationItem.titleView = button;}

运行程序,goto secondView,运行效果

下篇文件讲下Navigation 的Toobar如何显示和如何自己定义。

[转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. JavaScript学习笔记-实例详解-类(二)

    实例详解-类(二)   //===给Object.prototype添加只读\不可枚举\不可配置的属性objectId(function(){ Object.defineProperty(Object ...

  7. IOS开发之UINavigationController详解

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

  8. [转]iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用

    转载地址:http://www.mamicode.com/info-detail-514151.html 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载 ...

  9. UINavigationController详解一(转)UIBarButtonItem

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

随机推荐

  1. javascript 函数返回值(return)、定时器(setTimeout、setInterval)

    一.函数的返回值:return 1.函数名+括号=return后面的值 <script> function fn1(){ return 100; } alert(fn1()); // 10 ...

  2. jquery-mockjax初试

    1. 原理 jquery-mockjax是用于mock 前台ajax向后台请求的返回数据. 原理很简单 在你js代码要发送ajax请求的地方断点一下,然后比较在[引入jquery-mockjax] 和 ...

  3. JAX-WS(一)之使用wsgen从Java创建简单的WebService

    概念 JAX-WS2.0的全称Java API for XML-Based Web Service 2.0.JAX-WS2.0是对JAX-RPC1.0规范的扩展,是JAX-RPC1.1的后续版本,JA ...

  4. HTML语言的一些元素(五)

    一.table表格语法与结构 <table> <tr> <td> </td> <td> </td> <td> < ...

  5. 前端bower使用

    Bower是一个客户端技术的软件包管理器,是由twitter推出的.它可用于搜索.安装和卸载如JavaScript.HTML.CSS之类的网络资源.其他一些建立在Bower基础之上的开发工具,如Yeo ...

  6. PostgreSQL Replication之第十一章 使用Skytools(4)

    11.4 使用 londiste 复制数据 pgq是一个叫做londiste的复制工具的核心.londiste 的核心是有一个比如比Slony 更加简单,容易使用的机制.如果您在一个大的安装中使用Sl ...

  7. JQuery权限管理

    <title></title> <script src="JS/jquery-1.7.1.js"></script> <scr ...

  8. [转]SecureCRT使用配置详细图文教程

        Secure CRT是一款支持 SSH2.SSH1.Telnet.Telnet/SSH.Relogin.Serial.TAPI.RAW 等协议的终端仿真程序,最吸引我的是,SecureCRT ...

  9. java将数组中的零放到末尾

    package com.shb.java; /** * 将数组中的0放到数组的后边,然后原来的非零数的顺序不改变 * @author BIN * */ public class Demo2{ publ ...

  10. 多屏复杂动画CSS技巧三则(转载)

    本文转载自: 经验分享:多屏复杂动画CSS技巧三则