ios UINavigationController“后退按钮”自定义文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1441699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:24:33  来源:igfitidea点击:

UINavigationController "back button" custom text?

iosiphoneuinavigationcontroller

提问by Ali Shafai

The "back button" of a UINavigationControllerby default shows the title of the last view in the stack. Is there a way to have custom text in the back button instead?

UINavigationController默认情况下,a 的“后退按钮”显示堆栈中最后一个视图的标题。有没有办法在后退按钮中使用自定义文本?

回答by rein

From this link:

这个链接

self.navigationItem.backBarButtonItem =
   [[UIBarButtonItem alloc] initWithTitle:@"Custom Title"
            style:UIBarButtonItemStylePlain
           target:nil
           action:nil];

As Tyler said in the comments:

正如泰勒在评论中所说:

don't do this in the visible view controller, but in the view controller that you'd see if you hit the back button

不要在可见视图控制器中执行此操作,而是在您点击后退按钮时看到的视图控制器中执行此操作

回答by Petr Peller

You can set the text in the Interface Builder:

您可以在 Interface Builder 中设置文本:

Select the navigation item of the ViewController that the back button would return to:

选择返回按钮将返回到的 ViewController 的导航项:

enter image description here

在此处输入图片说明

In the utilities panel attribute inspector, enter your label for the Back Button:

在实用程序面板属性检查器中,输入您的后退按钮标签:

enter image description here

在此处输入图片说明

I would prefer this approach over setting the title in code as in the accepted answer.

我更喜欢这种方法,而不是像在接受的答案中那样在代码中设置标题。

Also note, you need to do this in the view controller one level up the stack. In other words, don't do this in the visible view controller, but in the view controller that you'd see if you hit the back button.
--Tyler

另请注意,您需要在堆栈上一级的视图控制器中执行此操作。换句话说,不要在可见视图控制器中执行此操作,而是在您点击后退按钮时看到的视图控制器中执行此操作。
——泰勒

回答by Trang

I use this:

我用这个:

// In the current view controller, not the one that is one level up in the stack
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.backItem.title = @"Custom text";
}

回答by Aubrey Goodman

I found a handy solution to this by simply setting the title of the controller before pushing another controller onto the stack, like this:

通过在将另一个控制器推入堆栈之前简单地设置控制器的标题,我找到了一个方便的解决方案,如下所示:

self.navigationItem.title = @"Replacement Title";
[self.navigationController pushViewController:newCtrl animated:YES];

Then, make sure to set the original title in viewWillAppear, like this:

然后,确保在 中设置原始标题viewWillAppear,如下所示:

-(void)viewWillAppear:(BOOL)animated
{
  ...
  self.navigationItem.title = @"Original Title";
  ...
}

This works because the default behavior of UINavigationControllerwhen constructing the back button during a push operation is to use the title from the previous controller.

这是有效的,因为UINavigationController在推送操作期间构造后退按钮时的默认行为是使用前一个控制器的标题。

回答by AddisDev

The title of the back button defaults to the previous view's title so a quick trick I use is to place the following code on the previous view's .m file.

后退按钮的标题默认为上一个视图的标题,因此我使用的一个快速技巧是将以下代码放在上一个视图的 .m 文件中。

-(void)viewWillAppear:(BOOL)animated {

    // Set title
    self.navigationItem.title=@"Original Title";
}

-(void)viewWillDisappear:(BOOL)animated {

    // Set title
    self.navigationItem.title=@"Back";
}

回答by valvoline

in your init method, add the following code:

在您的 init 方法中,添加以下代码:

- (id)initWithStyle:(UITableViewStyle)style {
    if(self = [super init]) {

        //...

        UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                                             style:UIBarButtonItemStylePlain 
                                            target:self 
                                            action:@selector(goBack)];
        self.navigationItem.leftBarButtonItem = customBackButton;
        [customBackButton release];

        //...
    }
    return self;
}

then add a simple method, to allow viewcontroller dismissing:

然后添加一个简单的方法,以允许视图控制器解除:

-(void)goBack {
    [self.navigationController popViewControllerAnimated:YES];  
}

回答by alones

Add the following code in viewDidLoad or loadView

在 viewDidLoad 或 loadView 添加以下代码

self.navigationController.navigationBar.topItem.title = @"Custom text";

I tested it in iPhone and iPad with iOS 9

我在装有 iOS 9 的 iPhone 和 iPad 上进行了测试

回答by Eric Goldberg

Adding to rein's answer. Note from Apple's docs that the declaration of backBarButtonItemis this:

添加到缰绳的答案。从 Apple 的文档中注意,backBarButtonItem的声明是这样的:

@property(nonatomic, retain) UIBarButtonItem *backBarButtonItem

Therefore, rein's answer will leak memory because the synthesized setter will retainthe instance you pass it, which is never released explicitly. You can remedy this by using autorelease

因此,rein 的答案会泄漏内存,因为合成的 setter 将retain传递您传递它的实例,而该实例永远不会显式释放。您可以使用以下方法解决此问题autorelease

 self.navigationItem.backBarButtonItem = 
      [[[UIBarButtonItem alloc] initWithTitle:@"Custom Title" 
         style:UIBarButtonItemStyleBordered
         target:nil
         action:nil] autorelease];  //<-- autoreleased

Or you could point a variable at the instance so you can explicitly release it later:

或者您可以在实例上指向一个变量,以便您稍后可以显式释放它:

UIBarButtonItem* item = ...
self.navigationItem.backBarButtonItem = item;
[item release];

Hope this helps!

希望这可以帮助!

回答by LCANT

- (void)viewDidLoad {
  [super viewDidLoad];

  UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
  self.navigationItem.backBarButtonItem = backButton;
  [backButton release];
}

回答by Dandré

I've discovered something interesting. If you subclass the UINavigationControllerand override the pushViewController:animated:method and do something like this: (bear in mind that I'm using ARC)

我发现了一些有趣的事情。如果您子类化UINavigationController并覆盖该pushViewController:animated:方法并执行以下操作:(请记住,我正在使用 ARC)

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
 initWithTitle: @"Back" 
 style: UIBarButtonItemStyleBordered
 target: nil action: nil];

viewController.navigationItem.backBarButtonItem = backButton;

[super pushViewController:viewController animated:animated];

Then for all ViewControllersthat are pushed with your navigation controller will have the "Back" button in them automatically. If you want to change the text for certain view controllers you can try and maybe cast the viewcontroller to a certain class or your own custom protocol (which your viewcontroller inherits from which could have a method like backButtonTextor something silly like that) which can give you certain information on the viewcontroller that's coming in sothat you can customize the back button text for it. Now the back button text is taken care of in a place which should hold the responsibility solely. I have to admit that creating a new button to change the text sucks, but oh well.

然后,对于ViewControllers使用导航控制器推送的所有内容,它们将自动包含“返回”按钮。如果您想更改某些视图控制器的文本,您可以尝试将视图控制器强制转换为某个类或您自己的自定义协议(您的视图控制器继承自该协议可能有类似backButtonText或类似的方法),它可以给你传入的视图控制器上的某些信息,以便您可以为其自定义后退按钮文本。现在后退按钮文本被处理在一个应该单独负责的地方。我不得不承认,创建一个新按钮来更改文本很糟糕,但是很好。

Can anyone think of a reason why not to do it like this? Atleast you don't have to fiddle with viewcontroller titles or have to remember to create a new back button before pushing the viewcontroller on the navigation controller.

谁能想出一个不这样做的理由?至少您不必摆弄视图控制器标题或必须记住在导航控制器上按下视图控制器之前创建一个新的后退按钮。