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

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

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

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练习的差不多了。

前面两篇:

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

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

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

 
著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢
责任编辑:闫佳明 来源: csdn
相关推荐

2013-04-02 10:15:45

iOS学习UINavigatio添加UIBarButt

2013-04-02 10:16:34

iOS学习UINavigatio页面切换

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

2011-08-02 11:17:13

iOS开发 View

2011-08-03 17:32:17

IOS UIScrollVi touch

2010-08-12 13:21:31

华为3COMIOS

2019-02-12 15:04:09

2019-01-04 15:14:18

2011-08-23 13:56:12

MySQLConnection

2014-07-23 08:55:42

iOSFMDB

2011-08-16 16:10:12

MySQLORDER BY子句GROUP BY子句

2011-08-16 15:35:50

MySQLSELECT语句FROM子句

2022-04-27 08:17:07

OCMock单元测试集成

2013-12-17 11:04:10

iOS开发传感器

2021-08-25 07:43:17

AndroidSurfaceViewTextureView

2010-07-06 10:56:32

UML图详解

2013-01-30 15:36:03

NFC移动支付蓝牙
点赞
收藏

51CTO技术栈公众号