原文出自:http://blog.csdn.net/totogo2010/article/details/7682641,特别感谢。

1、显示Toolbar 

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

  1. [self.navigationController  setToolbarHidden:NO animated:YES];

2、在ToolBar上添加UIBarButtonItem

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

  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添加

  1. #import <UIKit/UIKit.h>
  2. @interface SecondViewController : UIViewController
  3. {
  4. UIToolbar *toolBar;
  5. }
  6. @end

在SecondViewController.m添加

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. [self.navigationController  setToolbarHidden:YES animated:YES];
  5. UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(gotoThridView:)];
  6. 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)];
  7. [toolBar setBarStyle:UIBarStyleDefault];
  8. toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
  9. [toolBar setItems:[NSArray arrayWithObject:addButton]];
  10. [self.view addSubview:toolBar];
  11. // Do any additional setup after loading the view from its nib.
  12. }

先把RootView时显示的Toobar隐藏

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

[toolBarsetItems:[NSArrayarrayWithObject:addButton]];

BarButtonItem用 的是UIBarButtonSystemItemSearch, 效果如下:

4、新建ThridView,从SecondView跳转到

Commad+N新建一个ThridViewController,

这个addButton跳转到ThridView

  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. }

跳转Second到Third效果:

到此UINavigationController练习的差不多了。

UINavigationController详解三(转)ToolBar的更多相关文章

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

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

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

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

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

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

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

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

  5. UINavigationController详解一(转)UIBarButtonItem

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

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

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

  7. .NET DLL 保护措施详解(三)最终效果

    针对.NET DLL 保护措施详解所述思路完成最终的实现,以下为程序包下载地址 下载 注意: 运行环境为.net4.0,需要安装VS2015 C++可发行组件包vc_redist.x86.exe.然后 ...

  8. Android 之窗口小部件详解(三)  部分转载

    原文地址:http://blog.csdn.net/iefreer/article/details/4626274. (一) 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget ...

  9. WebSocket安卓客户端实现详解(三)–服务端主动通知

    WebSocket安卓客户端实现详解(三)–服务端主动通知 本篇依旧是接着上一篇继续扩展,还没看过之前博客的小伙伴,这里附上前几篇地址 WebSocket安卓客户端实现详解(一)–连接建立与重连 We ...

随机推荐

  1. Swift-11-协议(Protocols)

    协议定义了一个蓝图,规定了用来实现某一特定工作或者功能所必须的方法和属性.类.结构体或者枚举类型都可以遵循协议,并提供具体实现来完成协议定义的方法和功能.任意能满足协议要求的类型被称为遵循confor ...

  2. Javascript数据类型的一些注意点

    1.字符串类型 substring()返回指定索引区间的子串: var s = 'hello, world' s.substring(0, 5); // 从索引0开始到5(不包括5),返回'hello ...

  3. The command 'new_value' for SQLPlus

    Format: column column_name new_value var_name Meaning: use the column_name of a select statment to c ...

  4. C# WebBrowser控件 模拟登录 抓取数据

    参考博客:C#中的WebBrowser控件的使用 参考博客:C#中利用WebBrowser控件,获得HTML源码 一.问题点: 1.模拟登录后,如果带有嵌套的iframe嵌套,不好读取iframe内容 ...

  5. Java foreach

    foreach循环也叫增强型的for循环,或者叫foreach循环. foreach循环是JDK5.0的新特性(其他新特性比如泛型.自动装箱等). import java.util.Arrays; p ...

  6. checkbox的完美用户体验(转)

    如需查看效果-->自行建个html文件,或者-->原文:http://bbs.blueidea.com/thread-2711834-1-7.html 最常见的checkbox的使用: & ...

  7. JS函数创建的具体过程

    JS函数创建的过程: 1.新建Object对象F,类型设置为Function 2.设置F.__proto__ = Function.prototype 3.设置F.constructor = Func ...

  8. SVN提交.a文件的方法

    选择菜单View-->Ignored Files,这样就会显示出ignored的文件,找到你要上传的.a文件,右键“Add”就可以了.

  9. ClassLoader,Thread.currentThread().setContextClassLoader,tomcat的ClassLoader

    实际上,在Java应用中所有程序都运行在线程里,如果在程序中没有手工设置过ClassLoader,对于一般的java类如下两种方法获得的ClassLoader通常都是同一个 this.getClass ...

  10. MVC3 FAQ

    1. html helper 可以缩写代码的,下面2段代码是相同效果 @Html.DropDownList("TourList") <select name="aa ...