UINavigationController(3)

1、顯示Toolbar 

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

  1. [self.navigationController  setToolbarHidden:NO animated:YES];  
    [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]];  
UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
    UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];
    UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
    UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [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.   
  3. @interface SecondViewController : UIViewController  
  4. {  
  5.     UIToolbar *toolBar;  
  6. }  
  7. @end  
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
{
    UIToolbar *toolBar;
}
@end

在SecondViewController.m添加

  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. }  
- (void)viewDidLoad
{
    [super viewDidLoad];

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

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(gotoThridView:)];
    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)];
    [toolBar setBarStyle:UIBarStyleDefault];
    toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [toolBar setItems:[NSArray arrayWithObject:addButton]];
    [self.view addSubview:toolBar];
     
    // Do any additional setup after loading the view from its nib.
}
先把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.   
  7. }  
-(void)gotoThridView:(id)sender
{
    ThridViewController *thridView = [[ThridViewController alloc] init];
    [self.navigationController pushViewController:thridView animated:YES];
    thridView.title = @"Thrid View";

}

跳轉Second到Third效果:


到此UINavigationController練習的差不多了。

前面兩篇:

iOS學習之UINavigationController詳解與使用(一)添加UIBarButtonItem

iOS學習之UINavigationController詳解與使用()頁面切換和segmentedController  


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

著作權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者博客鏈接,謝謝

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章