2012年10月3日 星期三

UINavigationBar的基本使用(一)

一個最基本的動態NavigationBar的設定,並使用兩個Button來做移除及設定動態NavigationBar的顯示。

Xcode版本4.5 & IOS6

1. 開啓一個專案

2. 設定StoryBoard並加入一個Label及兩個Button

3. 設定dynamicNavBarViewController.h

#import <UIKit/UIKit.h>

@interface dynamicNavBarViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property (weak, nonatomic) IBOutlet UIButton *showButton;
@property (weak, nonatomic) IBOutlet UIButton *delButton;

@property (nonatomic) UINavigationBar *myNavBar;

@property (nonatomic) UINavigationItem *NavTitle;

@property (nonatomic) UIBarButtonItem *myBarItem11;
@property (nonatomic) UIBarButtonItem *myBarItem12;
@property (nonatomic) UIBarButtonItem *myBarItem13;

-(void)myNavAction1;
-(void)myNavAction2;
-(void)myNavAction3;


@end

4.  將程式碼加入到設定dynamicNavBarViewController. m
#import "dynamicNavBarViewController.h"

@interface dynamicNavBarViewController ()

@end

@implementation dynamicNavBarViewController

@synthesize myNavBar;

@synthesize myBarItem11, myBarItem12, myBarItem13;

@synthesize NavTitle;

@synthesize showLabel;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)showNavBar:(id)sender {
   
    [self setupMyNavBar];
   
    [self.view  addSubview:myNavBar];
}
- (IBAction)delNavBar:(id)sender {
   
    [self removeAllSubView];
}

-(void)setupMyNavBar
{
    myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 44.0)];
   
    NavTitle = [[UINavigationItem alloc] initWithTitle:@"NavBar 測試"];
   
    [myNavBar  pushNavigationItem:NavTitle animated:YES];
   
    myBarItem11 = [[UIBarButtonItem alloc]
                   initWithTitle:@"左按钮"
                   style:UIBarButtonItemStylePlain
                   target:self
                   action:@selector(myNavAction1)];
   
    NavTitle.leftBarButtonItem = myBarItem11;
   
    myBarItem12 = [[UIBarButtonItem alloc]
                   initWithTitle:@"右按钮"
                   style:UIBarButtonItemStylePlain
                   target:self
                   action:@selector(myNavAction2)];
   
    myBarItem13 = [[UIBarButtonItem alloc]
                   initWithTitle:@"右二按钮"
                   style:UIBarButtonItemStylePlain
                   target:self
                   action:@selector(myNavAction3)];   
  
    NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myBarItem12,myBarItem13,nil];   
   
    NavTitle.rightBarButtonItems = toolbarItems;
   
}

- (void) removeAllSubView
{
    for(UIView *subview in [self.view subviews]) {
       
        if([subview isKindOfClass:[UINavigationBar class]]) {
            [subview removeFromSuperview];
        }
       
    }
}


-(void)myNavAction1
{
    NSString *text = @"測試左鍵";
   
    showLabel.text = text;
}

-(void)myNavAction2
{
    NSString *text = @"測試右鍵";
   
    showLabel.text = text;
}

-(void)myNavAction3
{
    NSString *text = @"測試右二鍵";
   
    showLabel.text = text;
}



@end

5. 執行結果





沒有留言: