IOS学习之UINavigationController详解与使用(三)ToolBar

简介:
IOS学习之UINavigationController详解与使用()页面切换和segmentedController   接上篇,我们接着讲Navigation 的Toolbar。


1、显示Toolbar 

在RootViewController.m的- (void)viewDidLoad方法中添加代码,这样Toobar就显示出来了。

[cpp]  view plain copy
  1. [self.navigationController  setToolbarHidden:NO animated:YES];  


2、在ToolBar上添加UIBarButtonItem

新建几个UIBarButtonItem,然后以数组的形式添加到Toolbar中

[cpp]  view plain copy
  1. UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];  
  2.     UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];  
  3.     UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];  
  4.     UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];  
  5.     UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];  
  6.     [self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];  

效果:



注意:用   [self.navigationController.toolbar setItems:(NSArray *) animated:<#(BOOL)#>]这个方法添加item是不起效果的。下面我动态自己添加Toolbar时,这个才起效果。


3、动态添加Toolbar

我们在SecondView添加动态的Toolbar。

在SecondViewController.h添加

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface SecondViewController : UIViewController  
  4. {  
  5.     UIToolbar *toolBar;  
  6. }  
  7. @end  

在SecondViewController.m添加

[cpp]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     [self.navigationController  setToolbarHidden:YES animated:YES];  
  6.   
  7.     UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(gotoThridView:)];  
  8.     toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - toolBar.frame.size.height - 44.0, self.view.frame.size.width, 44.0)];  
  9.     [toolBar setBarStyle:UIBarStyleDefault];  
  10.     toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;  
  11.     [toolBar setItems:[NSArray arrayWithObject:addButton]];  
  12.     [self.view addSubview:toolBar];  
  13.        
  14.     // Do any additional setup after loading the view from its nib.  
  15. }  
先把RootView时显示的Toobar隐藏

    [self.navigationController setToolbarHidden:YESanimated:YES];然后把新建的Toolbar添加的SecondView中,并为Toobar设置了一个Item.  

    [toolBarsetItems:[NSArrayarrayWithObject:addButton]]; 

BarButtonItem用 的是UIBarButtonSystemItemSearch, 效果如下:


4、新建ThridView,从SecondView跳转到

Commad+N新建一个ThridViewController,

这个addButton跳转到ThridView

[cpp]  view plain copy
  1. -(void)gotoThridView:(id)sender  
  2. {  
  3.     ThridViewController *thridView = [[ThridViewController alloc] init];  
  4.     [self.navigationController pushViewController:thridView animated:YES];  
  5.     thridView.title = @"Thrid View";  
  6.   
  7. }  

跳转Second到Third效果:


到此UINavigationController练习的差不多了。

前面两篇:

IOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

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


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

原博客链接地址http://blog.csdn.net/totogo2010/




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


相关文章
|
3月前
|
前端开发 开发工具 Swift
学习iOS开发的准备
准备学习iOS开发?确保有Mac和最新Xcode,先学好编程基础特别是Swift。利用Apple官方文档、在线课程和书籍作为资源。熟悉Xcode及Git,通过实践项目和开源代码积累经验。深研架构模式、核心框架和优化技巧。加入开发者社区,关注行业动态,持续学习。
34 1
|
11月前
|
安全 前端开发 Android开发
鸿蒙开发|鸿蒙系统的介绍(为什么要学习鸿蒙开发|鸿蒙系统的官方定义|鸿蒙和安卓、ios的对比)
鸿蒙开发学习是一项探索性的工作,旨在开发一个全场景分布式操作系统,覆盖所有设备,让消费者能够更方便、更直观地使用各种设备。
569 6
鸿蒙开发|鸿蒙系统的介绍(为什么要学习鸿蒙开发|鸿蒙系统的官方定义|鸿蒙和安卓、ios的对比)
|
iOS开发
iOS UIKit Dynamics Demo 学习地址列表
iOS UIKit Dynamics Demo 学习地址列表
46 0
|
XML JSON 编解码
IM通讯协议专题学习(九):手把手教你如何在iOS上从零使用Protobuf
接上篇《金蝶随手记团队的Protobuf应用实践(原理篇)》,本文将以iOS端的Objective-C代码为例,图文并茂地向您菔救绾卧趇OS工程中快速使用Protobuf,希望对你有帮助。
166 0
IM通讯协议专题学习(九):手把手教你如何在iOS上从零使用Protobuf
|
iOS开发
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
143 0
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
|
存储 Unix 编译器
|
存储 算法 iOS开发
|
存储 缓存 算法
iOS底层学习——对象初始化探索
iOS底层学习——对象初始化探索
|
物联网 Android开发 iOS开发
iOS开发 - 蓝牙学习的总结
iOS开发 - 蓝牙学习的总结
171 0
|
UED iOS开发
iOS开发UINavigation系列四——导航控制器UINavigationController
iOS开发UINavigation系列四——导航控制器UINavigationController
259 0