iOS学习之UINavigationController详解与使用(二)

移动开发 iOS
本文详细介绍了iOS学习之中的UINavigationController详解与使用,以及页面切换和segmentedController,希望对大家的iOS开发学习有所帮助。

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详解与使用(一)添加UIBarButtonItem

下篇:iOS学习之UINavigationController详解与使用(三)ToolBar

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

责任编辑:闫佳明 来源: csdn
相关推荐

2013-04-02 10:36:43

iOS学习UINavigatioToolBar

2013-04-02 10:15:45

iOS学习UINavigatio添加UIBarButt

2011-08-02 11:07:42

iOS开发 UIWebView

2015-07-09 13:47:37

IOSFMDB

2011-07-26 17:31:52

iOS 设计模式

2011-08-16 14:59:31

IOS开发ViewDidUnloiOS 5

2010-05-14 16:57:01

Subversion命

2011-08-02 11:17:13

iOS开发 View

2019-02-12 15:04:09

2010-08-12 13:21:31

华为3COMIOS

2011-08-03 17:32:17

IOS UIScrollVi touch

2011-08-16 16:10:12

MySQLORDER BY子句GROUP BY子句

2011-08-23 13:56:12

MySQLConnection

2019-01-04 15:14:18

2022-04-27 08:17:07

OCMock单元测试集成

2011-08-16 15:35:50

MySQLSELECT语句FROM子句

2011-08-01 15:41:50

IOS Core OS 框架

2013-01-30 15:36:03

NFC移动支付蓝牙

2012-01-18 13:46:37

ARCiOS

2021-08-25 07:43:17

AndroidSurfaceViewTextureView
点赞
收藏

51CTO技术栈公众号