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详解与使用(三)ToolBar

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢

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

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

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

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

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

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

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

  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. UINavigationController详解一(转)UIBarButtonItem

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

  9. IOS 友盟使用详解

    IOS 友盟使用详解 这篇博客将会详细介绍友盟的使用,希望对博友们有所帮助. 首先我们在浏览器上搜索友盟. 在这里我们选择官网这个,进去友盟官网后我们按照下图进行选择. 接下来选择如下图 Next 这 ...

随机推荐

  1. BZOJ4236 JOIOJI 【map】

    BZOJ4236 JOIOJI Description JOIOJI桑是JOI君的叔叔.“JOIOJI”这个名字是由“J.O.I”三个字母各两个构成的. 最近,JOIOJI桑有了一个孩子.JOIOJI ...

  2. BZOJ2005 NOI2010 能量采集 【莫比乌斯反演】

    BZOJ2005 NOI2010 能量采集 Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些 ...

  3. eclipse 无用代码扫描工具UCDetector

    本文主要介绍UCDetector(无用代码扫描工具)使用方法及局限 对于沉积或多方接手开发的项目经常会遇到一坨一坨无用的代码,但一个一个类查找又相当费时,这里介绍一个eclipse插件扫描没有引用的类 ...

  4. 《DSP using MATLAB》示例Example 8.26

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  5. asp.net远程调用WebService的两种方法(转载)

    一,静态方法在“解决方案‘项目名’” -> 相应的文件夹,如“Web References” ->右键“添加WEB引用”->在URL里写入地址.二,动态方法在“解决方案‘项目名’” ...

  6. [LeetCode系列]括号生成问题

    给定n, 返回所有匹配的n对括号的可能形式. 如 给定 n = 3, 一个解集是: "((()))", "(()())", "(())()" ...

  7. 关于tab的切换之共用html页面,而引发的页面跳转错乱问题

    在一个项目中的同一个模块中,有多个tab(并且多个tab对应的页面结构完全一样),tab的每次切换,不同tab调用不同的接口,利用一个switch进行判断,根据当前的类型去调用不同的接口,返回不同数据 ...

  8. jdk1.8新特性应用之Iterable

    我们继续看lambda表达式的应用: public void urlExcuAspect(RpcController controller, Message request, RpcCallback ...

  9. C#实现 OPC历史数据存取研究

    来源:http://blog.csdn.net/gjack/article/details/5641794 C#实现 OPC历史数据存取研究 (原文)Research of Accessing the ...

  10. 理解加密算法——创建CA机构,签发证书并开始TLS通信

    1 不安全的TCP通信 普通的TCP通信数据是明文传输的,所以存在数据泄露和被篡改的风险,我们可以写一段测试代码试验一下,NODE.JS代码: TCP Server: const net=requir ...