原文出自:http://blog.csdn.net/totogo2010/article/details/7682433,非常感谢。

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.leftBarButtonItem = backButton;

效果:

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,运行效果

UINavigationController详解二(转)页面切换和SegmentedController的更多相关文章

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

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

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

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

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

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

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

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

  5. UINavigationController详解一(转)UIBarButtonItem

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

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

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

  7. Shiro 安全框架详解二(概念+权限案例实现)

    Shiro 安全框架详解二 总结内容 一.登录认证 二.Shiro 授权 1. 概念 2. 授权流程图 三.基于 ini 的授权认证案例实现 1. 实现原理图 2. 实现代码 2.1 添加 maven ...

  8. .NET DLL 保护措施详解(二)关于性能的测试

    先说结果: 加了缓存的结果与C#原生代码差异不大了 我对三种方式进行了测试: 第一种,每次调用均动态编译 第二种,缓存编译好的对象 第三种,直接调用原生C#代码 .net dll保护系列 ------ ...

  9. PopUpWindow使用详解(二)——进阶及答疑

      相关文章:1.<PopUpWindow使用详解(一)——基本使用>2.<PopUpWindow使用详解(二)——进阶及答疑> 上篇为大家基本讲述了有关PopupWindow ...

随机推荐

  1. table表格某一td内容太多导致样式混乱的解决方案

    对于有很多条目的数据,通常采用table元素来快速实现,某一个td的内容太多的话就会导致样式混乱难看. 解决方案 要让table的宽度固定可以给table元素设置table-layout:fixed; ...

  2. 简述Mesos API–files

    Below is a set of endpoints available on a Mesos agent. These endpoints are reachable at the address ...

  3. Linux脚本执行过程重定向

    Linux脚本执行过程重定向 一.bash调试脚本,并将执行过程重定向到指定文件 bash –x  shell.sh 2>&1 | tee shell.log

  4. Winform项目调用asp.net数据接口

    最近一个WPF项目需要改写成android项目,思路是在asp.net项目中编写一个通用接口,便于其它平台下调用数据.刚接触到这些东西的时候完全是一头雾水,最根本的原因是不明白网站中的一个网页,为什么 ...

  5. 关于ios导航控制器的知识总结

    关于ios导航控制器的知识总结 添加了导航控制器后: 1.一个导航控制器会有一个顶部导航栏navigationbar和一个底部工具栏toolbar,它们是导航控制器navC的属性.且导航栏默认是不隐藏 ...

  6. MVC项目实践,在三层架构下实现SportsStore-10,连接字符串的加密和解密

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  7. SwipeRefreshLayout实现上拉加载下拉刷新

    package com.example.swiperefreshlayoutdemo; import java.util.ArrayList;import java.util.HashMap; imp ...

  8. Login failed知多少

    说起Login failed我们首先会想起密码错误,但密码错误只是众多login failed中的一个,本篇将罗列各类login failed在ERRORLOG中的表现,以及如何提取这类错误信息.通过 ...

  9. [转][C++ 11]override and final - write clean and maintainable C++ code

    原文: http://arne-mertz.de/2015/12/modern-c-features-override-and-final/ Today I write about a pair of ...

  10. error MIDL2311 : statements outside library block are illegal in mktyplib compatability mode

    解决办法: 1. Do not use the /mktyplib203 switch unless you have to deal with legacy code dating back to ...