在 UITableView
我配置 self.navigationItem.leftBarButtonItem
用一个按钮。现在我想重新加载此按钮以更新此按钮上的文本,但它已打开 Navigation bar
。我可以更新 UITableView
通过
[self.tableView reloadData]
但如何更新导航栏?
更新:我没有初始化按钮的标题,但重新加载它。所以任何初始化代码(例如 self.navigationItem.leftBarButtonItem.title = @"balabala"
)不起作用,因为它不会立即生效。
只需更改leftBarButtonItem上的文本?
self.navigationItem.leftBarButtonItem.title = @"Back"; // or whatever text you want
尝试这个
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Title"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(OnClick:)];
self.navigationItem.leftBarButtonItem = btnBack;
希望这有助于你..
编辑: -
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithTitle: @"Title" style: UIBarButtonItemStyleBordered target:self action:@selector(OnClick:)];
[[self navigationItem] setBackBarButtonItem: btnBack];
尝试这个:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:@"your_title"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(backBtnPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
您尝试使用设置文本吗?
self.navigationItem.leftBarButtonItem.title
您需要使用以下语句:
self.navigationItem setLeftBarButtonItem:cancelButton animated:YES];
换句话说,你需要setLeftBarButtonItem:动画 消息而不是标准消息。这将使用动画重新加载按钮文本。如果您有兴趣在某个时刻更改,则同样适用于后退按钮。
@interface Myviewcontroller : UIViewController
{
UIBarButtonItem *barBtnBack;
UIButton *btnQuestion;
}
// viewDidLoad中
btnQuestion = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 25)];
[btnQuestion setBackgroundImage:[UIImage imageNamed:@"question-mark2x"] forState:UIControlStateNormal];
[btnQuestion addTarget:self action:@selector(goBacktomain)
forControlEvents:UIControlEventTouchUpInside];
barBtnBack =[[UIBarButtonItem alloc] initWithCustomView:btnQuestion];
[self.navigationItem setLeftBarButtonItem:barBtnBack];
注意 - 当您想要更改navigationnar按钮标题时更改btnQuestion标题
如果要更改NavigationBar标题,请尝试以下方法之一:
[self.navigationController.navigationItem setTitle:@"Title"];
[self.navigationItem setTitle:@"Title"];
[self setTitle:@"Title"];
如果您想更改按钮标题,请将其放在同一个类中:
- (void)setLeftBarButtonTitle:(NSString *)title action:(SEL)selector
{
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
[self.navigationItem setLeftBarButtonItem:leftButton];
}
- (void)setRightBarButtonTitle:(NSString *)title action:(SEL)selector
{
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
[self.navigationItem setRightBarButtonItem:rightButton];
}
设置按钮标题和操作的示例:
// Change button titles and actions any time
[self setLeftBarButtonTitle:@"Back" action:@selector(goBack)];
[self setRightBarButtonTitle:@"Something" action:@selector(doSomething:)];
// Change button titles with no actions
[self setLeftBarButtonTitle:@"Nothing" action:nil];
[self setRightBarButtonTitle:@"Nothing" action:nil];
// Remove buttons from navigation bar
[self.navigationItem setLeftBarButtonItem:nil];
[self.navigationItem setRightBarButtonItem:nil];