【问题标题】:Change title of UINavigationBar更改 UINavigationBar 的标题
【发布时间】:2012-09-20 13:23:51
【问题描述】:

我创建了一个基于视图的应用程序。我在视图和两个按钮上放置了一个导航栏。我想在按下按钮后更改导航栏的标题。在 IB 中,我将按钮与相应的操作连接起来。调试显示操作正在被调用,但导航栏的标题没有改变。

请帮忙!

//
//  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

【问题讨论】:

  • 请使用标签指定此问题涉及的技术。如果您使用现有标签,例如php、.net等

标签: uinavigationbar title


【解决方案1】:

我找到了。我使用了 UINavigationItem 并将 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

【讨论】:

    猜你喜欢
    • 2015-10-04
    • 1970-01-01
    • 2012-05-25
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多