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/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢

在View中添加一个UIToolBar,其中代码如下:

 

//定义UIToolBar

m_pToolBar= [[UIToolbaralloc]initWithFrame:CGRectMake(0.0,self.view.frame.size.height-m_pActionButton.frame.size.height- 44.0,self.view.frame.size.width,44.0)];

[m_pToolBar setBarStyle:UIBarStyleBlackOpaque];

m_pToolBar.autoresizingMask=UIViewAutoresizingFlexibleTopMargin;


//向uitoolbar添加控件

 

m_pTextField=[[UITextFieldalloc]initWithFrame:CGRectMake(12.0,7.0,264.0,31.0)];

m_pTextField.delegate= self;

[m_pTextField setBorderStyle:UITextBorderStyleRoundedRect];

[m_pTextField setFont:[UIFont fontWithName:@"Helvetica" size:16.0]];

m_pButtonAdd= [UIButton buttonWithType:UIButtonTypeCustom];

[m_pButtonAdd setFrame:CGRectMake(286.0,8.0,29.0,29.0)];

[m_pButtonAdd addTarget:selfaction:@selector(addOpinion)forControlEvents:UIControlEventTouchUpInside];

[m_pButtonAdd setImage:[UIImage imageNamed:@"button_plus"]forState:UIControlStateNormal];

 

UIBarButtonItem *mBarButtonItemTextField =[[UIBarButtonItemalloc]initWithCustomView:m_pTextField];

UIBarButtonItem *mBarButtonItemButtonAdd =[[UIBarButtonItemalloc]initWithCustomView:m_pButtonAdd];

[m_pToolBarsetItems:[NSArrayarrayWithObjects:mBarButtonItemTextField,mBarButtonItemButtonAdd,nil]];

[mBarButtonItemTextFieldrelease];

[mBarButtonItemButtonAddrelease];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值