简体   繁体   English

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

[英]UINavigationController “back button” custom text?

The "back button" of a UINavigationController by default shows the title of the last view in the stack. 默认情况下, UINavigationController的“后退按钮”显示堆栈中最后一个视图的标题。 Is there a way to have custom text in the back button instead? 有没有办法在后退按钮中使用自定义文本?

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 不要在可见视图控制器中执行此操作,而是在视图控制器中执行此操作,如果您单击后退按钮

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的导航项:

在此输入图像描述

In the utilities panel attribute inspector, enter your label for the Back Button: 在实用工具面板属性检查器中,输入后退按钮的标签:

在此输入图像描述

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 --Tyler

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";
}

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 UINavigationController when constructing the back button during a push operation is to use the title from the previous controller. 这是因为在推送操作期间构造后退按钮时UINavigationController的默认行为是使用前一个控制器的标题。

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";
}

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: 然后添加一个简单的方法,以允许viewcontroller解雇:

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

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上测试过

I've discovered something interesting. 我发现了一些有趣的东西。 If you subclass the UINavigationController and 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 ViewControllers that are pushed with your navigation controller will have the "Back" button in them automatically. 然后,对于使用导航控制器推送的所有ViewControllers ,将自动在其中包含“后退”按钮。 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 backButtonText or 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. 如果你想更改某些视图控制器的文本,你可以尝试将viewcontroller转换为某个类或你自己的自定义协议(你的viewcontroller从哪个继承可能有像backButtonText这样的方法或类似的东西),这可以给出你在viewcontroller上的某些信息,你可以自定义它的后退按钮文本。 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. 我不得不承认创建一个新的按钮来改变文字糟透了,但是哦。

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. 至少你不必摆弄视图控制器标题,或者必须记住在按下导航控制器上的视图控制器之前创建一个新的后退按钮。

Adding to rein's answer. 添加到缰绳的答案。 Note from Apple's docs that the declaration of backBarButtonItem is this: 请注意Apple的文档中backBarButtonItem的声明是这样的:

@property(nonatomic, retain) UIBarButtonItem *backBarButtonItem

Therefore, rein's answer will leak memory because the synthesized setter will retain the instance you pass it, which is never released explicitly. 因此,rein的答案会泄漏内存,因为合成的setter将retain您传递的实例,而这个实例永远不会被明确释放。 You can remedy this by using autorelease 您可以使用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! 希望这可以帮助!

- (void)viewDidLoad {
  [super viewDidLoad];

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

Put this into you viewDidLoad , hope it will result into what you are looking for 把它放到你的viewDidLoad ,希望它会产生你想要的东西

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

Expanding on Aubrey's suggestion, you can do this in the child view controller: 扩展Aubrey的建议,您可以在子视图控制器中执行此操作:

create two variables for storing the old values of the parent's navigationItem.title and the parent's navigationItem 创建两个变量来存储父级的navigationItem.title和父级的navigationItem的旧值

UINavigationItem* oldItem;
NSString* oldTitle;

in viewDidLoad , add the following: viewDidLoad ,添加以下内容:

oldItem = self.navigationController.navigationBar.topItem;  
oldTitle = oldItem.title;  
[oldItem setTitle: @"Back"];  

in viewWillDisappear , add the following: viewWillDisappear ,添加以下内容:

[oldItem setTitle: oldTitle];  
oldTitle = nil;  // do this if you have retained oldTitle
oldItem = nil;   // do this if you have retained oldItem

It's not perfect. 这不完美。 You will see the the title of the parent view change as the new controller is animated in. BUT this does achieve the goal of custom labeling the back button and keeping it shaped like a standard back button. 当新控制器被动画化时,您将看到父视图的标题更改。但这确​​实实现了自定义标记后退按钮并使其形状像标准后退按钮的目标。

if You want to set title in ARRIVING controller (sometimes more logic..) in swift 3 do: 如果你想在swift 3中设置ARRIVING控制器中的标题(有时候更多逻辑..):

func setBackButtonNavBar(title: String, delay: Double){

    let when = DispatchTime.now() + delay
    DispatchQueue.main.asyncAfter(deadline: when, execute: { () -> Void in

        if let navBar = self.navigationController?.navigationBar{
            navBar.backItem?.title = title
        }
    })

}

in upcoming controller: 即将到来的控制器

override func viewDidLoad() {
    super.viewDidLoad()
    self.setBackButtonNavBar(title: "back", delay: 0.3)
}

usually I put self.setBackButtonNavBar in a controller extension. 通常我将self.setBackButtonNavBar放在控制器扩展中。

I know this is an old question and the answers' kind of out updated! 我知道这是一个老问题,答案有点不断更新!

The easy way is to do this in parent ViewController: 简单的方法是在父ViewController中执行此操作:

ie the one that takes you to next view controller. 即带你到下一个视图控制器的那个。

self.navigationItem.backBarButtonItem  = UIBarButtonItem(title: "Custom text here", style: .plain, target: nil, action: nil)

rein's answer works well. rein的答案很有效。

Note that if you push more than one view controller, the changed back button title will appear for each of them, which may not be what you want. 请注意,如果您推送多个视图控制器,则会为每个视图控制器显示更改的后退按钮标题,这可能不是您想要的。

In that case, you'll need to create the custom UIBarButtonItem each time you push a view controller. 在这种情况下,每次推送视图控制器时都需要创建自定义UIBarButtonItem。

Also, make sure you do it before pushing the view controller, otherwise you will get a screen hiccup as the title changes. 此外,请确保按下视图控制器之前执行此操作,否则在标题更改时将出现屏幕打嗝。

Doing this in code remove the back button style of the UINavigationConroller . 在代码中执行此操作将删除UINavigationConroller的后退按钮样式。 If you add a Navigation Item in each of yours views, you can set the title of the back botton in the StoryBoard . 如果在每个视图中添加导航项,则可以在StoryBoard设置后边框的标题。

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

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