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

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

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

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

我该怎么办?

谢谢。


当前回答

斯坦的回答是最好的。但它也有一个问题,当你使用带有标签栏的控制器并更改控制器的标题时,你也可以更改标签栏的标题。所以最好的答案是改变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;
  }
}

其他回答

斯威夫特版本:

在你的子ViewController中:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.backItem?.title = "TEXT"
}

这应该放在调用名为NewTitle的ViewController的方法中。 就在push或popViewController语句之前。

UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];

这是另一种方法。

在你的父视图控制器中,实现以下方法:

- (void) setBackBarButtonItemTitle:(NSString *)newTitle {
  self.navigationItem.backBarButtonItem.title = newTitle;
}

在你的子视图控制器中,当你想要改变标题时,这将会工作:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];

我从来没有能够让parentViewController属性工作:

[(ParentViewController *)(self.navigationController.parentViewController) setBackBarButtonItemTitle:@"New Title"];

我不知道这是一个错误还是我没有正确使用它。但是在viewControllers数组中抓取倒数第二个视图控制器指向父视图控制器,我可以用那个引用正确地调用父方法。

iOS 11+解决方案-不需要再次创建后退按钮。

将backButtonTitle设置为前一屏幕上的一个空格。

// If navigation is from A -> B, set in A's `viewDidLoad`.
navigationItem.backButtonTitle = " "

也许我过于简单了,但从苹果的文档中,措辞是:

如果一个自定义栏按钮项没有被任何一个视图控制器指定,则使用一个默认的返回按钮,并且它的标题被设置为前一个视图控制器的title属性的值——也就是说,在堆栈的下一层的视图控制器。

上面标记为正确的解决方案设置了来自父控制器的默认按钮项。这是正确的答案,但我正在通过改变自我来解决问题。在将新控制器推送到NavigationController堆栈之前,UIViewController的title属性。

这将自动更新后退按钮在下一个控制器上的标题,只要你设置了self。标题回到什么它应该在viewWillAppear我不能看到这个方法造成太多的问题。