ios 如何更改后退按钮上的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/835607/
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 15:54:10  来源:igfitidea点击:

How to change text on a back button

iosiphonecocoa-touchuinavigationitem

提问by Ilya Suzdalnitski

By default the back button uses as a text on it a title of a viewcontroller. Can I change text on the back button without changing a title of a view controller? I need this because I have a view controller which title is too long to display and in this case I would like to display just "Back" as a caption for back button.

默认情况下,后退按钮使用视图控制器的标题作为文本。我可以在不更改视图控制器标题的情况下更改后退按钮上的文本吗?我需要这个,因为我有一个视图控制器,它的标题太长而无法显示,在这种情况下,我只想显示“后退”作为后退按钮的标题。

I tried the following which didn't work:

我尝试了以下不起作用的方法:

self.navigationItem.leftBarButtonItem.title = @"Back";

Thanks.

谢谢。

回答by Marc W

Try

尝试

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

I found that by looking at the backBarButtonItemdocs in Apple's docs for UINavigationItem.

我通过查看Apple 的UINavigationItem文档中的backBarButtonItem文档发现了这一点。

回答by Marco

Marc W's approach worked great once I figured out which controller to apply it to: the one being re-titled, not the one on top. So if this is the navigation stack:

Marc W 的方法在我确定将其应用于哪个控制器后效果很好:重新命名的控制器,而不是顶部的控制器。所以如果这是导航堆栈:

(bottom)  ControllerA -> ControllerB  (top)

...and you want to give a shorter title for ControllerA in the back-button displayed when ControllerB is on top, you apply the property change to ControllerA.

...并且您想在ControllerB 位于顶部时显示的后退按钮中为 ControllerA 提供更短的标题,您将属性更改应用于ControllerA

So it's more in the context of self.title, not like the other left/right-bar-button setters.

所以它更多是在 的上下文中self.title,而不是像其他左/右栏按钮设置器。

回答by alyoka

You can do it in the storyboard. Find the view controller you want to go back to (the one with the long title), select it's Navigation Item, and open the Attributes Inspector (Alt+Cmd+4), insert the custom Back Button title.

您可以在故事板中进行。找到要返回的视图控制器(标题较长的那个),选择它的导航项,然后打开属性检查器(Alt+Cmd+4),插入自定义后退按钮标题。

enter image description here

在此处输入图片说明

回答by Ben Call

Thanks Marco... that helped me...

谢谢马可……这对我有帮助……

Here is what i did.

这是我所做的。

If you are using a tableView to navigate to different views... put the code:

如果您使用 tableView 导航到不同的视图...放置代码:

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

In your didSelectRowAtIndexPathmethod... of the first Controller... Controller A.

在你的didSelectRowAtIndexPath方法中......第一个控制器......控制器A。

When you navigate to Controller B the button will have the title "Back".

当您导航到控制器 B 时,按钮的标题为“返回”。

回答by Rose Perrone

The back button pulls its text from the title of the parent view controller.

后退按钮从父视图控制器的标题中提取其文本。

In the parent view controller (the view controller that appears when you tap the back button), set its own title as the desired text on the back button.

在父视图控制器(点击后退按钮时出现的视图控制器)中,将其自己的标题设置为后退按钮上所需的文本。

For example, let's say we have a RootViewControllerclass. When we click a cell in its table view, we push an instance of SecondViewController. We want the back button of the SecondViewControllerinstance to read, "Home."

例如,假设我们有一个RootViewController类。当我们在表格视图中点击一个单元格时,我们推送了一个SecondViewController. 我们希望SecondViewController实例的后退按钮显示为“主页”。

in the viewDidLoadmethod of RootViewController.m:

RootViewController.mviewDidLoad方法中:

self.title = @"Home";

in the viewDidLoadmethod of SecondViewController.m:

SecondViewController.mviewDidLoad方法中:

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

If you want your back button to read, "Back," set the title of the parent view controller to @"Back";

如果您希望后退按钮显示为“后退”,请将父视图控制器的标题设置为@"Back"

回答by Felipe FMMobile

This work better for me. Try :

这对我来说更好。尝试 :

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

回答by AntonijoDev

If you are using storyboard:

如果您使用故事板:

  1. Open StoryBoard
  2. In Document Outline window find ViewController to which you want to return to
  3. Click on Navigation Item of that ViewController
  4. In Attributes explorer change Back Button value to your custom tile
  1. 打开故事板
  2. 在文档大纲窗口中找到要返回到的 ViewController
  3. 单击该 ViewController 的导航项
  4. 在属性资源管理器中将后退按钮值更改为您的自定义磁贴

That is it, enjoy...

就是这样,享受...

回答by BillF

And in MonoTouch the following works (in ViewDidLoad of the parent controller):

在 MonoTouch 中,以下工作(在父控制器的 ViewDidLoad 中):

NavigationItem.BackBarButtonItem = new UIBarButtonItem( "Back", UIBarButtonItemStyle.Plain, null);

回答by user1004911

[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc]
  initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]];

This worked for me.

这对我有用。

回答by Zorayr

In your parent view controller, set the back button when view loads:

在您的父视图控制器中,在视图加载时设置后退按钮:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    self.navigationItem.backBarButtonItem = 
       [[UIBarButtonItem alloc] initWithTitle:@"title" 
                                        style:UIBarButtonItemStylePlain 
                                       target:nil 
                                       action:nil];

}

Notice that we don't need to include autoreleaseat the end with the latest iOS version.

请注意,我们不需要autorelease在最后包含最新的 iOS 版本。

Hope this helps!

希望这可以帮助!