13

UITableViewself.navigationItem.leftBarButtonItem用一个按钮配置。现在我想重新加载此按钮以更新此按钮上的文本,但它在Navigation bar. UITableView我可以更新

 [self.tableView reloadData]

但是如何更新导航栏?

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

4

8 回答 8

3

只需更改 leftBarButtonItem 上的文本?

self.navigationItem.leftBarButtonItem.title = @"Back"; // or whatever text you want
于 2013-02-02T11:24:40.407 回答
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];
于 2013-02-02T11:26:09.680 回答
1

尝试这个:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"your_title"
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(backBtnPressed:)];
    self.navigationItem.leftBarButtonItem = backButton;
于 2015-04-24T09:29:57.580 回答
1

您是否尝试设置文本,使用

self.navigationItem.leftBarButtonItem.title
于 2013-02-02T11:27:22.877 回答
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];
于 2015-04-23T15:31:03.957 回答
1

您需要使用以下语句:

self.navigationItem setLeftBarButtonItem:cancelButton animated:YES];  

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

于 2013-05-27T21:35:53.333 回答
1
 @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];

注意-当您想更改导航按钮标题时更改 btnQuestion 标题

于 2015-04-23T14:50:31.223 回答
0

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

navigationController?.loadView()

于 2020-05-17T10:12:52.310 回答