博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
阅读量:4107 次
发布时间:2019-05-25

本文共 2410 字,大约阅读时间需要 8 分钟。

是上篇,我们接着讲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] 
  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] 
  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] 
  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] 
  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] 
  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如何显示和如何自己定义。

转载地址:http://zsvsi.baihongyu.com/

你可能感兴趣的文章
PHP7新特性 What will be in PHP 7/PHPNG
查看>>
比较strtr, str_replace和preg_replace三个函数的效率
查看>>
ubuntu 下编译PHP5.5.7问题:configure: error: freetype.h not found.
查看>>
PHP编译configure时常见错误 debian centos
查看>>
configure: error: Please reinstall the BZip2 distribution
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
【增强学习在无人驾驶中的应用】
查看>>
OpenCV meanshift目标跟踪总结
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
都无代码了,还要程序员吗?
查看>>
面试想拿 10K,HR 说我只配7k?
查看>>
那些人生“开挂”的程序员,都在干什么?
查看>>
影响科学圈的那些计算机代码
查看>>
乐视视频 App 图标改为“欠 122 亿”,网友:我在别家分红包,却在你家随份子!...
查看>>
为何程序员总喜欢写技术博客,看完恍然大悟...
查看>>
如何判断一家互联网公司要倒闭了?
查看>>
想快速上手机器学习?来看下这个 GitHub 项目!
查看>>
GitHub 标星 3.6k,一本开源的深度学习中文教程!
查看>>
9 款你不能错过的 JSON 工具
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>