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

发布于 2024-07-20 16:29:24 字数 247 浏览 10 评论 0原文

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

我尝试了以下方法,但没有成功:

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

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

蘑菇王子 2024-07-27 16:29:24

尝试

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

我发现通过查看 backBarButtonItem 文档在 Apple 文档中的 UINavigationItem

Try

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

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

好倦 2024-07-27 16:29:24

当我弄清楚将其应用到哪个控制器时,Marc W 的方法效果非常好:重新命名的控制器,而不是顶部的控制器。 因此,如果这是导航堆栈:

(bottom)  ControllerA -> ControllerB  (top)

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

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

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:

(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.

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

夜无邪 2024-07-27 16:29:24

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

在此处输入图像描述

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.

enter image description here

美煞众生 2024-07-27 16:29:24

谢谢马可...这帮助了我...

这就是我所做的。

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

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

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

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

Thanks Marco... that helped me...

Here is what i did.

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

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

In your didSelectRowAtIndexPath method... of the first Controller... Controller A.

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

温柔戏命师 2024-07-27 16:29:24

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

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

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

RootViewController.mviewDidLoad方法中:

self.title = @"Home";

SecondViewController.mviewDidLoad方法中:

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

如果你想要你的返回按钮读取“返回”,将父视图控制器的标题设置为@“返回”

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 RootViewController class. When we click a cell in its table view, we push an instance of SecondViewController. We want the back button of the SecondViewController instance to read, "Home."

in the viewDidLoad method of RootViewController.m:

self.title = @"Home";

in the viewDidLoad method of SecondViewController.m:

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

深者入戏 2024-07-27 16:29:24

这对我来说效果更好。 尝试 :

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

This work better for me. Try :

 self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] 
initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
二手情话 2024-07-27 16:29:24

如果您使用 Storyboard:

  1. 在 Document Outline 窗口中打开 StoryBoard
  2. ,找到您想要的 ViewController
    返回到
  3. 单击该 ViewController 的导航项
  4. 在属性资源管理器中将后退按钮值更改为您的自定义磁贴

就是这样,享受...

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

That is it, enjoy...

许久 2024-07-27 16:29:24

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

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

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

NavigationItem.BackBarButtonItem = new UIBarButtonItem( "Back", UIBarButtonItemStyle.Plain, null);
绝對不後悔。 2024-07-27 16:29:24
[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc]
  initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]];

这对我有用。

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

This worked for me.

逐鹿 2024-07-27 16:29:24

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

- (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];

}

请注意,在最新的 iOS 版本中,我们不需要在末尾包含 autorelease

希望这可以帮助!

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 autorelease at the end with the latest iOS version.

Hope this helps!

清音悠歌 2024-07-27 16:29:24

[self.navigationController.navigationBar.backItem setTitle:@"back"];

它对我有用。 您可以将“返回”替换为其他内容。

[self.navigationController.navigationBar.backItem setTitle:@"back"];

It works for me. You can replace "back" with something else.

残月升风 2024-07-27 16:29:24

如果你不想拥有头衔,这个对我有用!

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

This one worked for me if you don't want to have a title!

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil];
终难愈 2024-07-27 16:29:24
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
情归归情 2024-07-27 16:29:24

我终于知道为什么这些答案一开始对我不起作用。 我在故事板中设置了标题。 当我在代码上设置标题时。 有用!

self.navigationItem.title = @"Main Menu";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];

I finally knew why these answers did not work for me at first. I set the title in storyboard. When i set the title on code. it works!

self.navigationItem.title = @"Main Menu";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
季末如歌 2024-07-27 16:29:24

我的解决方案是在视图控制器被推送到导航堆栈时设置标题,并在推送 vc 关闭之前使用委托方法重置它:

因此,当我推送其他视图控制器时,我将标题更改放在调用视图控制器中,例如:

self.pushedVC = [self.storyboard instantiateViewControllerWithIdentifier:@"pushedVCIdentifier"];
self.pushedVC.delegate = self;   
[self.navigationController pushViewController:self.pushedVC animated:YES];
self.title = @"Back";

并在委托中回调函数(我在 viewWillDissapear 中调用):

-(void)pushedVCWillClose:(PushedVC *)sender
{
    self.title = @"Previous Title";
}

My solution was to set title when the view controller is pushed to navigation stack and reset it by use of delegate method before pushed vc closes:

So I put the title change in calling view controller when I push the other view controller like:

self.pushedVC = [self.storyboard instantiateViewControllerWithIdentifier:@"pushedVCIdentifier"];
self.pushedVC.delegate = self;   
[self.navigationController pushViewController:self.pushedVC animated:YES];
self.title = @"Back";

and in delegate callback function (which I invoke in viewWillDissapear):

-(void)pushedVCWillClose:(PushedVC *)sender
{
    self.title = @"Previous Title";
}
剪不断理还乱 2024-07-27 16:29:24

如果您不仅想更改“后退”按钮的文本并保持原始的左箭头形状,而且还想在用户单击“后退”按钮时执行某些操作,我建议您看看我的“CustomNavigationController"。

If you want not only to change the text of the Back button and remain the original left-arrow shape, but also to do something when user clicks the Back button, I recommend you to have a look around my "CustomNavigationController".

欢你一世 2024-07-27 16:29:24

//您可以通过在上一个视图控制器中设置标题来实现此目的,如下所示

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //Set Title for this view
        self.navigationItem.title = "My Title"

}

override func viewWillDisappear(animated: Bool) {
        super.viewWillAppear(animated)
        //Set Title for back button in next view 
        self.navigationItem.title = "Back"
}

//You can achieve this by setting the title in the previous view controller as shown below

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //Set Title for this view
        self.navigationItem.title = "My Title"

}

override func viewWillDisappear(animated: Bool) {
        super.viewWillAppear(animated)
        //Set Title for back button in next view 
        self.navigationItem.title = "Back"
}
殊姿 2024-07-27 16:29:24

要更改后退按钮标题,请参阅下面的代码

InformationVC *infoController=[[InformationVC alloc]init];[self.navigationController infoController animated:YES];

//Below code changed back button title on InformationVC page.
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Information" style: UIBarButtonItemStylePlain target: nil action: nil];
self.navigationItem.backBarButtonItem = backBarButton;`enter code here`

For to change back button title, refer below code

InformationVC *infoController=[[InformationVC alloc]init];[self.navigationController infoController animated:YES];

//Below code changed back button title on InformationVC page.
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Information" style: UIBarButtonItemStylePlain target: nil action: nil];
self.navigationItem.backBarButtonItem = backBarButton;`enter code here`
柠檬心 2024-07-27 16:29:24

在 Swift5 中,backBarButtom 无法编辑。 因此,我们需要先隐藏backBarButtom,然后使用自定义的leftbarButtom来替换backBarButtom。 这是详细的解决方案:https://stackoverflow.com/a/63868300/13939003

In Swift5, the backBarButtom cannot be edited. Hence, we need to hide the backBarButtom first, then use a customised leftbarButtom to replace backBarButtom. Here is the detailed solution: https://stackoverflow.com/a/63868300/13939003

惟欲睡 2024-07-27 16:29:24

这对我有用:

self.navigationController.navigationBar.topItem.title = "Back"

This worked for me:

self.navigationController.navigationBar.topItem.title = "Back"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文