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

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

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

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

我该怎么办?

谢谢。


当前回答

问题:导航栏中的“返回”文本不能被替换。

原因:在推送视图后,导航栏中设置了“Back”标签,因为父视图控制器中的.title属性被设置为nil(或未初始化)。

一个解决方案:如果你设置self.title="Whatever…",你会看到在按下新的视图控制器后,"Back"将出现"Whatever…"。

其他回答

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

在Xcode 4.5中使用storyboard,到目前为止,当Back按钮的值不需要动态改变时,我发现最简单的解决方案是使用“Back button”字段与视图控制器的导航项相关联,你想让“Back”按钮表示其他内容。

例如,在下面的截图中,我想要视图控制器的后退按钮(s),我推有“后退”作为后退按钮的标题。

当然,如果你每次都需要返回按钮显示稍微不同的内容,这就行不通了……这里有所有其他的解决方案。

如果你不仅想将后退按钮的文本更改为相同的文本并保持原来的左箭头形状,而且还想在用户单击后退按钮时做一些事情,我建议你看看我的“CustomNavigationController”。

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

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

这样对我更好。试一试:

 self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] 
initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];