如何重新加载导航栏

时间:2013-02-02 11:20:09

标签: ios uitableview uinavigationbar uibarbuttonitem

UITableView中,我使用按钮配置self.navigationItem.leftBarButtonItem。现在我想重新加载此按钮以更新此按钮上的文本,但它位于Navigation bar上。我可以通过

更新UITableView
 [self.tableView reloadData]

但如何更新导航栏?

更新:我没有初始化按钮的标题,但重新加载它。所以任何初始化代码(例如self.navigationItem.leftBarButtonItem.title = @"balabala")都不起作用,因为它不会立即生效。

8 个答案:

答案 0 :(得分:2)

只需更改leftBarButtonItem上的文字?

self.navigationItem.leftBarButtonItem.title = @"Back"; // or whatever text you want

答案 1 :(得分:2)

试试这个

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];

答案 2 :(得分:1)

您是否尝试使用

设置文字
self.navigationItem.leftBarButtonItem.title

答案 3 :(得分:1)

您需要使用以下声明:

self.navigationItem setLeftBarButtonItem:cancelButton animated:YES];  

换句话说,您需要setLeftBarButtonItem:动画消息而不是标准消息。这将使用动画重新加载按钮文本。同样适用于后退按钮,如果您有兴趣在某些时候进行更改。

答案 4 :(得分:1)

git branch -m trunk master

// viewDidLoad中

 @interface Myviewcontroller : UIViewController
{
       UIBarButtonItem *barBtnBack;
       UIButton *btnQuestion;
}

注意 - 当您想要更改navigationnar按钮标题时更改btnQuestion标题

答案 5 :(得分:1)

如果您想更改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];

答案 6 :(得分:1)

试试这个:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"your_title"
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(backBtnPressed:)];
    self.navigationItem.leftBarButtonItem = backButton;

答案 7 :(得分:0)

我在Swift中解决了它,但是如果显示了键盘,它就会隐藏起来。

navigationController?.loadView()