简体   繁体   English

在NavigationBar中更改后退按钮的标题

[英]Change the title of backbutton in NavigationBar

Following the examples of the many duplicates for this questions, I can't seem to get it right. 按照这个问题的许多重复的例子,我似乎无法做到正确。

I have a UINavigationViewController that has a LoginViewController as the rootViewController. 我有一个UINavigationViewController,它有一个LoginViewController作为rootViewController。 Here I got a button with a segue (push) to a LoginInfoViewController. 在这里,我得到一个带有segue(push)的按钮到LoginInfoViewController。

In LoginInfoViewController.m: 在LoginInfoViewController.m中:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //null
    NSLog(@"%@", self.navigationItem.backBarButtonItem);

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Test"
                                                                   style:UIBarButtonItemStyleDone
                                                                  target:nil
                                                                  action:nil];
    self.navigationItem.backBarButtonItem = backButton;

    //not null, still the back button says: "Back"
    NSLog(@"%@", self.navigationItem.backBarButtonItem);
}

在此输入图像描述

Set the title of the back button on the view BEFORE. 在BEFORE上设置后视图的标题。 So, if you segue from LoginViewController , you set the back button title on the item before you segue to LoginInfoViewController 因此,如果您从LoginViewController ,则在切换到LoginInfoViewController之前在项目上设置后退按钮标题

Example: 例:

In the viewDidLoad method on LoginViewController : LoginViewController上的viewDidLoad方法中:

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

This means you're setting the button on the LoginViewController , not on the LoginInfoViewController . 这意味着您要在LoginViewController上设置按钮,而不是在LoginInfoViewController上设置按钮。

You will need to set the backBarButtonItem of the controller you are going back to, not the controller you pushed. 您需要设置要返回的控制器的backBarButtonItem ,而不是您推送的控制器。 Move your code to the LoginViewController viewDidLoad method. 将代码移动到LoginViewController viewDidLoad方法。

The navigation controller derives the back button for the navigation bar from the backBarButtonItem of the preceding controller in the stack. 导航控制器从backBarButtonItem中前一个控制器的backBarButtonItem派生导航栏的后退按钮。 If the item is nil, it will use the value in the title property of same. 如果该项为nil,则它将使用title属性中的值。 If the title is too long to fit, the navigation bar may substitute the string "Back" in place of the title. 如果标题太长而不适合,导航栏可以替换字符串“Back”代替标题。 If your controller has a custom left bar button item, the navigation bar will ignore the backButtonItem property and title presenting the custom button instead. 如果您的控制器有一个自定义左栏按钮项,导航栏将忽略backButtonItem属性和标题,而是显示自定义按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM