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

简介:

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为下个页面返回按钮的名字。

[cpp]  view plain copy
  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添加如下代码:

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

3.2[segmentedController addTarget:selfaction:的实现

[cpp]  view plain copy
  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.         }  
  10.         break;  
  11.     case 1:  
  12.         {  
  13.             UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了排骨" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
  14.             [alter show];  
  15.         }  
  16.         break;  
  17.           
  18.         default:  
  19.             break;  
  20.     }  
  21. }  
这样就能响应鸡翅和排骨按钮了

4、自定义backBarButtonItem

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

代码如下:

[cpp]  view plain copy
  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中添加下面如下。

[cpp]  view plain copy
  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详解与使用(三)ToolBarhttp://blog.csdn.net/duxinfeng2010/article/details/7707072




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



原博客连接地址http://blog.csdn.net/totogo2010/article/details/7682433





     本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208753,如需转载请自行联系原作者




相关文章
|
5月前
|
安全 前端开发 Android开发
鸿蒙开发|鸿蒙系统的介绍(为什么要学习鸿蒙开发|鸿蒙系统的官方定义|鸿蒙和安卓、ios的对比)
鸿蒙开发学习是一项探索性的工作,旨在开发一个全场景分布式操作系统,覆盖所有设备,让消费者能够更方便、更直观地使用各种设备。
292 6
鸿蒙开发|鸿蒙系统的介绍(为什么要学习鸿蒙开发|鸿蒙系统的官方定义|鸿蒙和安卓、ios的对比)
|
7月前
|
iOS开发
iOS UIKit Dynamics Demo 学习地址列表
iOS UIKit Dynamics Demo 学习地址列表
23 0
|
iOS开发
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
128 0
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
|
物联网 Android开发 iOS开发
iOS开发 - 蓝牙学习的总结
iOS开发 - 蓝牙学习的总结
129 0
|
存储 Unix 编译器
|
存储 算法 iOS开发
|
存储 缓存 算法
iOS底层学习——对象初始化探索
iOS底层学习——对象初始化探索
|
UED iOS开发
iOS开发UINavigation系列四——导航控制器UINavigationController
iOS开发UINavigation系列四——导航控制器UINavigationController
226 0
|
iOS开发 Android开发 开发者
iOS 8 之后UINavigationController新特性
iOS 8 之后,UINavigationController 为开发者提供了一些好用的功能,这些功能以前实现起来可能比较麻烦,而现在只需要一个属性就搞定了。
842 0
|
API iOS开发 开发者
学习iOS开发如何进阶?
前言 如果你有志于将iOS开发作为职业,或者已经是一位iOS开发者,那么你应该听说过唐巧的名字。唐巧,2012年从网易有道离开参与创业, 目前是猿题库iOS高级研发工程师。
2169 0