首页 文章

更改UINavigationBar的 Headers

提问于
浏览
0

我已经创建了一个基于视图的应用程序 . 我在视图和两个按钮上放置了一个导航栏 . 按下按钮后,我想更改导航栏的 Headers . 在IB中,我将按钮与相应的动作连接起来 . 调试显示调用了操作,但导航栏的 Headers 未更改 .

请帮忙!

//
//  NavBarViewController.h

#import <UIKit/UIKit.h>

@interface NavBarViewController : UIViewController {

}

-(IBAction) clickedOne: (id)sender;
-(IBAction) clickedTwo: (id)sender;

@end
//
//  NavBarViewController.m

#import "NavBarViewController.h"

@implementation NavBarViewController

-(IBAction) clickedOne: (id)sender {
    self.navigationItem.title = @"1";
}

-(IBAction) clickedTwo: (id)sender {
    self.navigationItem.title = @"2";
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

1 回答

  • 1

    我找到了 . 我使用了UINavgationItem并将IB中的IBOutlet与UINavigationItem连接起来 .

    //
    //  NavBarViewController.h
    
    #import <UIKit/UIKit.h>
    
    @interface NavBarViewController : UIViewController {
      UINavigationItem *navBar;
    }
    
    @property (nonatomic, retain) IBOutlet UINavigationItem *navBar;
    
    -(IBAction) clickedOne: (id)sender;
    -(IBAction) clickedTwo: (id)sender;
    
    @end
    
    //
    //  NavBarViewController.m
    
    #import "NavBarViewController.h"
    
    @implementation NavBarViewController
    
    @synthesize navBar;
    
    -(IBAction) clickedOne: (id)sender {
        navBar.title = @"1";
    }
    
    -(IBAction) clickedTwo: (id)sender {
        navBar.title = @"2";
    }
    
    
    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    
        // Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
        [super dealloc];
    }
    
    @end
    

相关问题