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

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

我试着把下面这行代码放在视图控制器的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;
  }
}

其他回答

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

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

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

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

好的。我个人讨厌所有这些选择。因此我想出了我自己的。

根据我所看到的信息。看起来Previous视图控制器控制着它自己的“Back”按钮,该按钮将显示在被推的视图控制器上。

我已经为控制器上想要更改Back Button的navigationItem创建了一个Lazy Load方法。

我的是一个邀请买家控制器

邀请买家是默认设置的文本。

但后退按钮需要是“邀请”

下面是我用来创建后退按钮的代码。

我把这段代码放在Controller's Implementatio (.m)文件的顶部,它会自动覆盖super的方法。

- (UINavigationItem *)navigationItem{
    UINavigationItem *item = [super navigationItem];
    if (item != nil && item.backBarButtonItem == nil)
    {
        item.backBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
        item.backBarButtonItem.title = @"Invite";
    }

    return item;
}

我觉得这是一种更优雅的方式。

我把这些代码放在一个地方,在需要时自动填充。

不需要在每个推送请求之前调用代码。

希望这能有所帮助

好的,就这么办。如果你有一个视图控制器“first”,你通过按按钮等方式导航另一个视图控制器“second”,你需要做一些工作。 首先,你需要在“second”视图控制器的ViewDidLoad方法中创建一个BarButtonItem,如下所示;

    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    [btnBack release];

之后,你需要在相同的.m文件中为“btnBack”操作编写代码;

-(IBAction)OnClick_btnBack:(id)sender  {
      [self.navigationController popViewControllerAnimated:YES];
    //[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];
}

这是所有。

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的期望和回来。

这在Swift 4和iOS 11中都适用。最常见的情况是,当用户点击表视图中的一行,一个新的视图控制器被推送到导航堆栈时,我需要这样做。通常我希望返回按钮是被点击的行所表示的对象的名称或某些属性。我通常在didSelectRowAt中这样做。是这样的:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let object = // get the object this row represents

    let backButton = UIBarButtonItem()
    backButton.title = object.name // or whatever
    self.navigationItem.backBarButtonItem = backButton

    // now show or segue to the next view controller
}