当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。

我想把按钮上显示的文本更改为其他内容。

我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。

self.navigationItem.leftBarButtonItem.title = @"Log Out";

我该怎么办?

谢谢。


当前回答

对于那些使用故事板的人,只需选择父视图(不是持有目标视图的那个)视图控制器框架(确保你在导航栏上单击右键,然后打开属性检查器,在那里你会发现三个表单输入。第三个“后退按钮”是我们正在寻找的。

其他回答

Most of solutions kills the original style of BackButton (The left arrowed bar button) while adding a usual button with desired title. So to keep the original style there are 2 ways: 1st: To use undocumented button style (110 or something like that) which I prefer not to do. But if you want you could find how to do it here, on stackoverflow. 2nd: To use I the Trenskow's idea. I liked it and I use it a bit changed. Instead of overriding - (NSString*)title I've decided to keep the original title in the following way (which allows me to use nib's titles as well as given title at push state btw).

- (void)viewDidLoad {
    [super viewDidLoad];
    static NSString * backButtonTitle=@"Back"; //or whatever u want

    if (![self.title isEqualToString:backButtonTitle]){

        UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
        customTitleView.text = self.title; // original title
        customTitleView.font = [UIFont boldSystemFontOfSize:20];
        customTitleView.backgroundColor = [UIColor clearColor];
        customTitleView.textColor = [UIColor whiteColor];
        customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
        customTitleView.shadowOffset = CGSizeMake(0, -1);

        [customTitleView sizeToFit];

        self.navigationItem.titleView = [customTitleView autorelease];
        self.title = backButtonTitle; 
    }
}

这个解决方案效果很好,看起来很自然。此外,如果在viewDidLoad方法中使用它,它可以防止执行超过1次。 我也试过杰塞德克的解决方案,但看起来很糟糕。它导致可见的用户标题栏的变化,从原来的BackButton的期望和回来。

以下是答案:

在viewDidAppear:animated中(不是在viewDidLoad中)执行以下操作

- (void)viewDidAppear:(BOOL)animated
{
     [self.navigationController.navigationBar.backItem setTitle:@"anything"];

     // then call the super
     [super viewDidAppear:animated];
}

如果你想保持后退按钮的形状。

我知道,这个问题很老了,但我找到了一个很好的解决方案。

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Custom Title";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;

来自childView的作品!在iOS 7上测试。

斯坦的回答是最好的。但它也有一个问题,当你使用带有标签栏的控制器并更改控制器的标题时,你也可以更改标签栏的标题。所以最好的答案是改变view_controller。navigationitem。只使用view_controller.navigationItem。函数中的标题。 答案在这里:(使用ARC并将它们添加到视图的viewDidLoad中)

  static NSString * back_button_title=@"Back"; //or whatever u want
  if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
    UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
    custom_title_view.text = view_controller.navigationItem.title; // original title
    custom_title_view.font = [UIFont boldSystemFontOfSize:20];
    custom_title_view.backgroundColor = [UIColor clearColor];
    custom_title_view.textColor = [UIColor whiteColor];
    custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    custom_title_view.shadowOffset = CGSizeMake(0, -1);

    [custom_title_view sizeToFit];

    view_controller.navigationItem.titleView = custom_title_view;
    view_controller.navigationItem.title = back_button_title;
  }

在我自己的使用中,我使它成为一个这样的函数,只是在viewDidLoad中有一行代码的特性。

+ (void)makeSubViewHaveBackButton:(UIViewController*) view_controller{
  static NSString * back_button_title=@"Back"; //or whatever u want
  if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
    UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
    custom_title_view.text = view_controller.navigationItem.title; // original title
    custom_title_view.font = [UIFont boldSystemFontOfSize:20];
    custom_title_view.backgroundColor = [UIColor clearColor];
    custom_title_view.textColor = [UIColor whiteColor];
    custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    custom_title_view.shadowOffset = CGSizeMake(0, -1);

    [custom_title_view sizeToFit];

    view_controller.navigationItem.titleView = custom_title_view;
    view_controller.navigationItem.title = back_button_title;
  }
}

这对我来说是以前发布的答案的“简化”版本。

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];

backButton.title = @"Go Back";

self.navigationItem.backBarButtonItem = backButton;

记住把代码放在父视图控制器(例如,有你的表格视图或UITableViewController的视图),而不是子视图或细节视图(例如,UIViewController)。

你可以像这样简单地本地化返回按钮字符串:

backButton.title = NSLocalizedString(@"Back Title", nil);