navigation controller

1、程序框架框架

  一、程序結構函數

  

  二、storyboard工具

  一個navigation controller 僅保留最基礎部分,其餘刪除
測試

  

   根視圖設置爲view controller:flex

   

   

   另外兩個視圖:ui

   

2、主要代碼spa

  一、ViewController.m中的主要代碼.net

    1)- (void)viewDidLoad方法code

- (void)viewDidLoad {
    [super viewDidLoad];
    //根視圖導航欄左側和右側的兩個按鈕
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(selectRightAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;

    //自定義從下一個視圖左上角,「返回」根視圖的按鈕
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"後退" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem=backButton;
    
    //自定義根視圖下方的工具條,可進一步綁定各個按鈕的處理函數;只對當前視圖有效,切換到其餘視圖時爲空白
    [self.navigationController  setToolbarHidden:NO animated:YES];
    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]];
    
}

    2)導航條左側按鈕響應函數orm

-(void)selectLeftAction:(id)sender
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你點擊了導航欄左按鈕" delegate:self  cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
    [alter show];
}

    3)導航條右側按鈕響應函數

-(void)selectRightAction:(id)sender
{
    VCSecondView *vcSecondView;
    vcSecondView =[self.storyboard instantiateViewControllerWithIdentifier: @"second_view"];
    [self.navigationController pushViewController:vcSecondView animated:YES];
}

  二、VCSecondView.m中的主要代碼

    1)- (void)viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //自定義在導航條顯示的對第二個視圖對描述;
    //UINavigationController的title能夠用別的view替代,好比用UIButton UILable等,此處使用uibutton
    UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
    [button setTitle: @"自定義title" forState: UIControlStateNormal];
    [button sizeToFit];
    self.navigationItem.titleView = button;
    
    //導航欄右側按鈕
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(selectRightAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    //自定義從下一個視圖左上角,「返回」本視圖的按鈕
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"後退" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem=backButton;
}

    2)導航條右側按鈕響應函數

-(void)selectRightAction:(id)sender
{ 
    VCThirdView *vcThirdView;
    vcThirdView =[self.storyboard instantiateViewControllerWithIdentifier: @"third_view"];
    [self.navigationController pushViewController:vcThirdView animated:YES];
}

  二、VCThirdView.m中的主要代碼

     均是默認代碼

3、運行測試

  一、初始界面

  

  二、單擊根視圖左側按鈕

  

  三、單擊根視圖右側按鈕

  

  四、單擊第二個視圖右側按鈕

  

4、另外兩個功能:不經常使用

  在第二個視圖中操做

  一、分割功能:UISegmentedControl

     1)- (void)viewDidLoad添加以下代碼: 

- (void)viewDidLoad
{
  。。。
   NSArray *array = [NSArray arrayWithObjects:@"分割a",@"分割b", nil];
    UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];
    segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;
    
    [segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedController;  
  。。。  
}

    2)分割按鈕響應函數:

-(void)segmentAction:(id)sender
{
     switch ([sender selectedSegmentIndex]) {
         case 0:
         {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你點擊了分割a" delegate:self  cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
            [alter show];
         }
             break;
         case 1:
         {
             UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你點擊了分割b" delegate:self  cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil];
             [alter show];
         }
             break;
 
         default:
             break;
     }
}

    3)運行結果:

       

      

  二、動態加載自定義的toolbar

    1)VCSecondView.h

#import <UIKit/UIKit.h>

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

    2)- (void)viewDidLoad中添加以下代碼:

  UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:nil/*@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];

   3)運行結果:切換到第三個視圖過程當中右上角有黑色陰影

     

5、其餘

  導航欄按鈕圖標類型:

  

 

  參考:http://blog.csdn.net/totogo2010/article/details/7681879

相關文章
相關標籤/搜索