UINavigationController詳解三ToolBar

來源:互聯網
上載者:User

標籤:style   blog   http   get   strong   width   

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

1、顯示Toolbar 

在RootViewController.m的- (void)viewDidLoad方法中添加代碼,這樣Toobar就顯示出來了。

 

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


 

2、在ToolBar上添加UIBarButtonItem

建立幾個UIBarButtonItem,然後以數組的形式添加到Toolbar中

 

[cpp] view plaincopy 
  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 plaincopy 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface SecondViewController : UIViewController  
  4. {  
  5.     UIToolbar *toolBar;  
  6. }  
  7. @end  


在SecondViewController.m添加

 

 

[cpp] view plaincopy 
  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 plaincopy 
  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練習的差不多了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.