如何更改“背面”的标题导航栏上的按钮

发布于 2024-08-05 03:51:00 字数 247 浏览 8 评论 0原文

目前,左栏按钮的默认值是加载当前视图的视图的标题,换句话说,按下按钮(后退按钮)时要显示的视图。

我想将按钮上显示的文本更改为其他内容。

我尝试将以下代码行放入视图控制器的 viewDidLoad 方法中,但它似乎不起作用。

self.navigationItem.leftBarButtonItem.title = @"Log Out";

我应该怎么办?

谢谢。

Currently the left bar button default value is the title of the view that loaded the current one, in other words the view to be shown when the button is pressed (back button).

I want to change the text shown on the button to something else.

I tried putting the following line of code in the view controller's viewDidLoad method but it doesn't seem to work.

self.navigationItem.leftBarButtonItem.title = @"Log Out";

What should I do?

Thanks.

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

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

发布评论

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

评论(30

长途伴 2024-08-12 03:51:01

我们有两个 VC 的 A 和 B。

如果你想更改 B 中的标题,请在 A

- (IBAction)goToBViewController:(UIButton *)sender {

    BViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Your title here"
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:nil
                                                                 action:nil];
    [[self navigationItem] setBackBarButtonItem:newBackButton];
    [self.navigationController pushViewController:vc animated:NO];

}

Swift 4.1 Xcode 9.4中编写此代码

let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "VC"])
let newBackButton = UIBarButtonItem.init(title: "Your title here", style: UIBarButtonItemStyle.plain, target: nil, action: nil)
navigationController?.navigationBar.topItem?.backBarButtonItem = newBackButton
navigationController?.pushViewController(secondViewController!, animated: true)

We have two VC's A and B.

If you want to change title in B, write this code in A

- (IBAction)goToBViewController:(UIButton *)sender {

    BViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Your title here"
                                                                  style:UIBarButtonItemStylePlain
                                                                 target:nil
                                                                 action:nil];
    [[self navigationItem] setBackBarButtonItem:newBackButton];
    [self.navigationController pushViewController:vc animated:NO];

}

Swift 4.1 Xcode 9.4

let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "VC"])
let newBackButton = UIBarButtonItem.init(title: "Your title here", style: UIBarButtonItemStyle.plain, target: nil, action: nil)
navigationController?.navigationBar.topItem?.backBarButtonItem = newBackButton
navigationController?.pushViewController(secondViewController!, animated: true)
怀念你的温柔 2024-08-12 03:51:01

斯坦的回答是最好的。但它也有一个问题,当您将控制器与选项卡栏一起使用并更改控制器的标题时,您也可以更改选项卡栏的标题。所以最好的答案是仅更改 view_controller.navigationItem.title 并使用 view_controller.navigationItem函数中的.title。
答案在这里:(使用ARC并将它们添加到视图的viewDidLoad中)

  static NSString * back_button_title=@"Back"; //or whatever u want
  if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
    UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
    custom_title_view.text = view_controller.navigationItem.title; // original title
    custom_title_view.font = [UIFont boldSystemFontOfSize:20];
    custom_title_view.backgroundColor = [UIColor clearColor];
    custom_title_view.textColor = [UIColor whiteColor];
    custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    custom_title_view.shadowOffset = CGSizeMake(0, -1);

    [custom_title_view sizeToFit];

    view_controller.navigationItem.titleView = custom_title_view;
    view_controller.navigationItem.title = back_button_title;
  }

在我自己使用时,我将其设为这样的函数,只需在viewDidLoad中使用一行代码即可实现该功能。

+ (void)makeSubViewHaveBackButton:(UIViewController*) view_controller{
  static NSString * back_button_title=@"Back"; //or whatever u want
  if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
    UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
    custom_title_view.text = view_controller.navigationItem.title; // original title
    custom_title_view.font = [UIFont boldSystemFontOfSize:20];
    custom_title_view.backgroundColor = [UIColor clearColor];
    custom_title_view.textColor = [UIColor whiteColor];
    custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    custom_title_view.shadowOffset = CGSizeMake(0, -1);

    [custom_title_view sizeToFit];

    view_controller.navigationItem.titleView = custom_title_view;
    view_controller.navigationItem.title = back_button_title;
  }
}

Stan's answer was the best one. But it also have a problem, when you use the controller with a Tab Bar and change the controller's title, you could change the Tab Bar's title too.So the best answer is change the view_controller.navigationItem.title only and use the view_controller.navigationItem.title in the function.
Answer is here:(With ARC and add them into view's viewDidLoad)

  static NSString * back_button_title=@"Back"; //or whatever u want
  if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
    UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
    custom_title_view.text = view_controller.navigationItem.title; // original title
    custom_title_view.font = [UIFont boldSystemFontOfSize:20];
    custom_title_view.backgroundColor = [UIColor clearColor];
    custom_title_view.textColor = [UIColor whiteColor];
    custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    custom_title_view.shadowOffset = CGSizeMake(0, -1);

    [custom_title_view sizeToFit];

    view_controller.navigationItem.titleView = custom_title_view;
    view_controller.navigationItem.title = back_button_title;
  }

In myself use, I make it a function like this, just have the feature with one line code in the viewDidLoad.

+ (void)makeSubViewHaveBackButton:(UIViewController*) view_controller{
  static NSString * back_button_title=@"Back"; //or whatever u want
  if (![view_controller.navigationItem.title isEqualToString:back_button_title]){
    UILabel* custom_title_view = [[UILabel alloc] initWithFrame:CGRectZero];
    custom_title_view.text = view_controller.navigationItem.title; // original title
    custom_title_view.font = [UIFont boldSystemFontOfSize:20];
    custom_title_view.backgroundColor = [UIColor clearColor];
    custom_title_view.textColor = [UIColor whiteColor];
    custom_title_view.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    custom_title_view.shadowOffset = CGSizeMake(0, -1);

    [custom_title_view sizeToFit];

    view_controller.navigationItem.titleView = custom_title_view;
    view_controller.navigationItem.title = back_button_title;
  }
}
纵性 2024-08-12 03:51:01

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

If you want not only to change the text of the Back button to the same text 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-08-12 03:51:00

这应该放置在调用标题为“NewTitle”的 ViewController 的方法中。
就在push 或popViewController 语句之前。

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

This should be placed in the method that calls the ViewController titled "NewTitle".
Right before the push or popViewController statement.

UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];
看透却不说透 2024-08-12 03:51:00

在 ChildVC 中,这对我有用...

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

在 Swift 中也适用!

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

In ChildVC this worked for me...

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

Works in Swift too!

self.navigationController!.navigationBar.topItem!.title = "Back"
最舍不得你 2024-08-12 03:51:00

这是 backBarButtonItem 的文档:

“当此导航项紧邻顶部项目下方时
堆栈中,导航控制器派生出后退按钮
此导航项的导航栏。 [...] 如果你想
为后退按钮指定自定义图像或标题,您可以指定
自定义栏按钮项目(带有您的自定义标题或图像)到此
相反,财产。”

“父” 视图控制器):

self.title = @"Really Long Title";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Short" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

当任何其他视图控制器 B 位于导航堆栈顶部时,并且A位于其正下方,B的后退按钮将具有标题“Short”

Here is the documentation for backBarButtonItem:

"When this navigation item is immediately below the top item in the
stack, the navigation controller derives the back button for the
navigation bar from this navigation item. [...] If you want to
specify a custom image or title for the back button, you can assign a
custom bar button item (with your custom title or image) to this
property instead."

View Controller A (the "parent" view controller):

self.title = @"Really Long Title";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Short" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

When any other view controller B is on top of the navigation stack, and A is right below it, B's back button will have the title "Short".

羁绊已千年 2024-08-12 03:51:00

在使用故事板的 Xcode 4.5 中,到目前为止,当“后退”按钮的值不必动态更改时,我发现的最简单的解决方案是使用与您要访问的视图控制器的导航项关联的“后退按钮”字段。想要“后退”按钮说点别的东西。

例如,在下面的屏幕截图中,我希望我按下的视图控制器的后退按钮将“后退”作为后退按钮的标题。

在此处输入图像描述

当然,如果您需要后退按钮每次都说一些稍微不同的内容,那么这将不起作用......这里有所有其他解决方案。

in Xcode 4.5 using storyboard, by far the easiest solution i've found when the value of the Back button doesn't have to change dynamically is to use the "Back Button" field associated with the Navigation Item of the View Controller to which you want the "Back" button to say something else.

e.g. in the screenshot below, i want the Back button for the view controller(s) that i push to have "Back" as the title of the Back button.

enter image description here

of course, this won't work if you need the back button to say something slightly different each time … there are all of the other solutions here for that.

一花一树开 2024-08-12 03:51:00

我知道,这个问题很老了,但我找到了一个很好的解决方案。

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Custom Title";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;

从 childView 开始工作!使用 iOS 7 进行测试。

I know, the question is very old, but I found a nice solution.

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Custom Title";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;

Works from childView! Tested with iOS 7.

一抹苦笑 2024-08-12 03:51:00

也许我过于简单化了,但从苹果的文档来看,措辞是:

如果两个视图控制器均未指定自定义栏按钮项,则使用默认后退按钮,并将其标题设置为标题的值前一个视图控制器的属性,即堆栈中下一层的视图控制器。

上面标记为正确的解决方案设置了父控制器的默认按钮项。这是正确的答案,但我通过在将新控制器推送到 NavigationController 堆栈之前更改 UIViewController 的 self.title 属性来解决问题。

这会自动更新下一个控制器上的后退按钮标题,只要您将 self.title 设置回 viewWillAppear 中应有的内容,我就看不到此方法造成太多问题。

Maybe I'm being over simplistic but From Apple's documentation the wording is:

If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack.

The solution marked correct above sets a default button item from the parent controller. It's the right answer, but I'm solving the issue by changing self.title property of the UIViewController right before pushing the new controller onto the NavigationController stack.

This automatically updates the back button's title on the next controller, and as long as you set self.title back to what it should be in viewWillAppear I can't see this method causing too many problems.

诗化ㄋ丶相逢 2024-08-12 03:51:00

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

 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-08-12 03:51:00

在 Swift/iOS8 中,以下内容对我有用:

let backButton = UIBarButtonItem(
      title: "Back Button Text",
      style: UIBarButtonItemStyle.Bordered,
      target: nil,
      action: nil
);

self.navigationController.navigationBar.topItem.backBarButtonItem = backButton;

从 Felipe 的答案移植。

In Swift/iOS8, the following worked for me:

let backButton = UIBarButtonItem(
      title: "Back Button Text",
      style: UIBarButtonItemStyle.Bordered,
      target: nil,
      action: nil
);

self.navigationController.navigationBar.topItem.backBarButtonItem = backButton;

Ported from Felipe's answer.

凹づ凸ル 2024-08-12 03:51:00

好的,这就是方法。如果您“第一个”有一个视图控制器,并且通过按下按钮等导航另一个“第二个”视图控制器,您需要做一些工作。
首先,您需要在“第二个”视图控制器的 ViewDidLoad 方法中创建一个 BarButtonItem,如下所示;

    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    [btnBack release];

完成此操作后,您需要在同一个 .m 文件中编写“btnBack”操作的代码,如下所示;

-(IBAction)OnClick_btnBack:(id)sender  {
      [self.navigationController popViewControllerAnimated:YES];
    //[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];
}

就这样。

Ok, here is the way. If you have a view controller "first" and you navigate another view controller "second" by pushing a button or etc. you need to do some work.
First you need to create a BarButtonItem in "second" view controller's ViewDidLoad method like this;

    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    [btnBack release];

After you do that, you need to write to code for "btnBack" action in the same .m file like this;

-(IBAction)OnClick_btnBack:(id)sender  {
      [self.navigationController popViewControllerAnimated:YES];
    //[self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];
}

That's all.

差↓一点笑了 2024-08-12 03:51:00

我有一个父视图控制器,标题很长。这导致后退按钮文本渗入子视图控制器的标题中。

在尝试了一堆不同的解决方案之后,这就是我最终所做的(扩展@john.k.doe方法):

使用Xcode 7.2,Swift 2

  1. 在故事板中,添加一个Navigation Item视图控制器场景(不是子 VC)

navigation item

  1. 在新导航项属性检查器上,输入空格< 后退按钮 字段中的 /code> 字符。稍后会详细介绍这一点。

视图层次结构中的导航项< /a>

添加空格字符返回按钮字段

  1. 视图控制器中,添加以下代码:

代码片段:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    switch segue.destinationViewController {
    case is ChildViewController:
        navigationItem.backBarButtonItem?.title = ""
    default:
        navigationItem.backBarButtonItem?.title = "Full Parent Title"
    }
}

说明:

返回按钮属于父视图控制器。 Navigation Item 为您提供后退按钮的句柄,以便您可以在代码或 Storyboard 中设置标题。

注意:

如果将Navigation Item Back Button文本保留为默认空字符串,则后退按钮标题将变为“Back”。

其他方法也有效,为什么使用这个?:

虽然可以覆盖子视图控制器上的后退按钮标题,但在它已经在屏幕上短暂闪烁之前对其进行处理是一个挑战。

一些方法构建了一个新的后退按钮并覆盖现有的按钮。我确信它有效,并且在某些用例中可能是必要的。但我更喜欢尽可能利用现有的 API。

对于某些情况,更改父视图控制器的标题是最快的解决方案。但是,这会更改父标题,因此您必须管理状态。 Tab Bar Controller 的情况也会变得混乱,因为标题更改会对 Tab Bar Item 标题产生副作用。

I had a parent view controller with a really long title. This resulted in the back button text bleeding into the title of the child view controller.

After trying a bunch of different solutions, this is what I ended up doing (expanding on the @john.k.doe approach):

Using Xcode 7.2, Swift 2

  1. In the Storyboard, add a Navigation Item to the Parent View Controller scene (not the child VC)

navigation item

  1. On the Attributes Inspector of your new Navigation Item, type in a space character in the Back Button field. More on this later.

navigation item in View Hierarchy

add a space character to the Back Button field

  1. In the Parent view controller, add the following code:

snippet:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    switch segue.destinationViewController {
    case is ChildViewController:
        navigationItem.backBarButtonItem?.title = ""
    default:
        navigationItem.backBarButtonItem?.title = "Full Parent Title"
    }
}

Explanation:

The back button sort of belongs to the parent view controller. The Navigation Item gives you a handle to the back button, so you can set the title in code or in the Storyboard.

Note:

If you leave the Navigation Item Back Button text as the default empty string, the back button title will become "Back".

Other approaches work, why use this one?:

While it's possible to override the back button title on the child view controller, it was a challenge getting a handle to it until it had already flashed briefly on the screen.

Some of the approaches construct a new back button and override the existing one. I'm sure it works, and probably necessary in some use cases. But I prefer to leverage existing APIs when possible.

Changing the title of the parent view controller is the quickest solution for some situations. However, this changes the parent title so you have to manage state. Things also get messy with a Tab Bar Controller because title changes cause side effects with the Tab Bar Item titles.

无需解释 2024-08-12 03:51:00

对于使用故事板的用户,只需选择父视图控制器框架(而不是持有目标视图的视图控制器框架)(确保在导航栏上单击鼠标右键,然后打开属性检查器,您将在其中找到三个表单输入。第三个“后退按钮”就是我们正在寻找的。

For those using storyboards just select the parent (not the one that is holding target view) view controller frame (be sure you click right on the Navigation bar, then open attributes inspector, where you'll find three form inputs. The third one "back button" is that we are looking for.

心舞飞扬 2024-08-12 03:51:00

Swift 版本:

在你的子 ViewController 中:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.backItem?.title = "TEXT"
}

Swift version:

In your child ViewController:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.backItem?.title = "TEXT"
}
木森分化 2024-08-12 03:51:00

这是另一种方法。

在您的父视图控制器中,实现以下方法:

- (void) setBackBarButtonItemTitle:(NSString *)newTitle {
  self.navigationItem.backBarButtonItem.title = newTitle;
}

在您的子视图控制器中,当您想要更改标题时,这将起作用:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];

我永远无法让 parentViewController 属性起作用:

[(ParentViewController *)(self.navigationController.parentViewController) setBackBarButtonItemTitle:@"New Title"];

我不知道不知道这是一个错误还是我没有正确使用它。但是,获取 viewControllers 数组中倒数第二个视图控制器指向父视图控制器,我可以使用该引用正确调用父方法。

Here's another way to do it.

In your parent view controller, implement the following method:

- (void) setBackBarButtonItemTitle:(NSString *)newTitle {
  self.navigationItem.backBarButtonItem.title = newTitle;
}

In your child view controller, when you want to change the title, this will work:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];

I was never able to get the parentViewController property to work:

[(ParentViewController *)(self.navigationController.parentViewController) setBackBarButtonItemTitle:@"New Title"];

I don't know if that's a bug or I'm not using it properly. But grabbing the second-to-last view controller in the viewControllers array points to the parent view controller, and I can call parent methods correctly with that reference.

噩梦成真你也成魔 2024-08-12 03:51:00

好的。我个人讨厌所有这些选择。因此我想出了自己的。

根据我所看到的信息。看起来上一个视图控制器控制着它自己的“后退”按钮,该按钮将显示在推送的视图控制器上。

我已经为控制器上想要更改后退按钮的 navigationItem 创建了一个延迟加载方法。

我的是邀请买家控制器

邀请买家是默认设置的文本。

但后退按钮需要被邀请

这是我用来创建后退按钮的代码。

我将此代码放置在控制器的 Implementatio (.m) 文件的顶部,它会自动覆盖超级方法。

- (UINavigationItem *)navigationItem{
    UINavigationItem *item = [super navigationItem];
    if (item != nil && item.backBarButtonItem == nil)
    {
        item.backBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
        item.backBarButtonItem.title = @"Invite";
    }

    return item;
}

我觉得这是实现这一目标的更优雅的方式。

我将此代码放在一处,并在需要时自动填充。

无需在每次推送请求之前调用代码。

希望这有帮助

ok. I personally hated all of these options. Therefore I came up with my own.

Based on the information I have seen. It appears that the Previous view controller is in control of its own "Back" button that will be presented on the pushed view controller.

I have created a Lazy Load method for the navigationItem on the controller that wants the changed Back Button.

Mine is an Invite Buyer Controller

Invite Buyer is the text that is set by default.

but the back button needed to be Invite

Here is the code that I used to create the back button.

I placed this code in the top of the Controller's Implementatio (.m) file and it overrode the super's method automatically.

- (UINavigationItem *)navigationItem{
    UINavigationItem *item = [super navigationItem];
    if (item != nil && item.backBarButtonItem == nil)
    {
        item.backBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
        item.backBarButtonItem.title = @"Invite";
    }

    return item;
}

I feel this is a much more elegant way to accomplish this.

I place this code in one place, and it automatically gets populated when needed.

No need to call the code before each push request.

Hope this helps

洒一地阳光 2024-08-12 03:51:00

对于斯威夫特:

    // Rename back button
    let backButton = UIBarButtonItem(
        title: "Back",
        style: UIBarButtonItemStyle.Plain, // Note: .Bordered is deprecated
        target: nil,
        action: nil
    )
    self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton

For Swift:

    // Rename back button
    let backButton = UIBarButtonItem(
        title: "Back",
        style: UIBarButtonItemStyle.Plain, // Note: .Bordered is deprecated
        target: nil,
        action: nil
    )
    self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton
救星 2024-08-12 03:51:00
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    [btnBack release];
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Back" 
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    [btnBack release];
清音悠歌 2024-08-12 03:51:00

答案如下:

viewDidAppear:animated 中(不在 viewDidLoad 中)执行以下操作。

- (void)viewDidAppear:(BOOL)animated
{
     [self.navigationController.navigationBar.backItem setTitle:@"anything"];

     // then call the super
     [super viewDidAppear:animated];
}

如果您想保持后退按钮的形状,请

Here is the answer:

In viewDidAppear:animated (NOT in viewDidLoad) do the following

- (void)viewDidAppear:(BOOL)animated
{
     [self.navigationController.navigationBar.backItem setTitle:@"anything"];

     // then call the super
     [super viewDidAppear:animated];
}

That if you want to keep the shape of the back button.

无力看清 2024-08-12 03:51:00

这里解释的解决方案都不适合我。所以我所做的就是通过以下方式从我来自的场景中删除标题:

self.title = @"";

因此,当呈现新场景时,后面的文本不会出现。

我绝对同意这根本不是一个明确的解决方案,但是有效,并且没有任何解释对我有用。

None of the solutions explained here worked for me. So what I did was remove the title from the scene where I came from in the following way:

self.title = @"";

So when new scene is presented the back text does not appear.

I absoluty agree that this is not a clear solution at all, but worked and none of the explained worked for me.

您的好友蓝忘机已上羡 2024-08-12 03:51:00

我发现最好将导航堆栈中当前视图控制器的标题更改为后退按钮所需的文本,然后再推送到下一个视图控制器。

例如

self.navigationItem.title = @"Desired back button text";
[self.navigationController pushViewController:QAVC animated:NO];

,然后在 viewDidAppear 中将标题设置回原始 VC 所需的标题。瞧!

I've found that it is best to change the title of the current view controller in the navigation stack to the desired text of the back button before pushing to the next view controller.

For instance

self.navigationItem.title = @"Desired back button text";
[self.navigationController pushViewController:QAVC animated:NO];

Then in the viewDidAppear set the title back to the desired title for the original VC. Voila!

疯了 2024-08-12 03:51:00

我发现,更改后退按钮名称的最简单方法是将视图控制器标题设置为后退按钮的标题,然后将视图控制器导航项中的 titleView 替换为真实的自定义标签姓名。

像这样:

CustomViewController.m

@implementation CustomViewController

- (NSString*)title {
    return @"Back Button Title";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
    customTitleView.text = @"Navigation Bar Title";
    customTitleView.font = [UIFont boldSystemFontOfSize:20];
    customTitleView.backgroundColor = [UIColor clearColor];
    customTitleView.textColor = [UIColor whiteColor];
    customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    customTitleView.shadowOffset = CGSizeMake(0, -1);

    [customTitleView sizeToFit];

    self.navigationItem.titleView = [customTitleView autorelease];
}

@end

这将使 UINavigationBar 中的标题看起来就像是原生的。使视图控制器能够具有单独的标题和后退按钮标题。

对于视图控制器 A 和 B,A 负责告知其后退按钮应如何显示,而 B 则显示。

编辑:这也保持了后退按钮的原生外观(左箭头栏按钮项目)。

I've found, that the easiest way to change the name of the back button is to set the view controllers title to the title of the back button, and then replacing the titleView in the view controllers navigation item to a custom label with it's real name.

Like this:

CustomViewController.m

@implementation CustomViewController

- (NSString*)title {
    return @"Back Button Title";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
    customTitleView.text = @"Navigation Bar Title";
    customTitleView.font = [UIFont boldSystemFontOfSize:20];
    customTitleView.backgroundColor = [UIColor clearColor];
    customTitleView.textColor = [UIColor whiteColor];
    customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
    customTitleView.shadowOffset = CGSizeMake(0, -1);

    [customTitleView sizeToFit];

    self.navigationItem.titleView = [customTitleView autorelease];
}

@end

This will make your title in UINavigationBar look as if it was native. Giving the view controller the ability to have seperated title and back button title.

In the case of view controller A and B, A is responsible for telling how it's back button should look, while B is displayed.

EDIT: This also maintains the back button native look (The left arrowed bar button item).

奈何桥上唱咆哮 2024-08-12 03:51:00

这段代码也有效。将其放在导航控制器的根控制器上:

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

This code works too. Put this on the root controller of the navigation controller:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
南巷近海 2024-08-12 03:51:00

我是 iOS 新手,但我将提供覆盖导航控制器类的非常简单的答案。
我有简单的覆盖推送和弹出方法并保存以前的视图控制器的标题。抱歉粘贴到 js 块中。对于如何在正常代码块中传递它有点困惑。

#import "MyCustomNavController.h"


@implementation MyCustomNavController {

    NSString *_savedTitle;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated withBackBtnTitle:(NSString *)title {
    _savedTitle = self.topViewController.title;

    self.topViewController.title = title;
    [super pushViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {

    [self.viewControllers objectAtIndex:self.viewControllers.count - 2].title = _savedTitle;
    return [super popViewControllerAnimated:animated];
}

@end

Im new in iOS but I will provide my very simple answer of overriding the navigation controller class.
I have simple override the push and pop methods and save the title of previous view controller. Sorry for pasting in js block. Was little confused how to past it in normal code block.

#import "MyCustomNavController.h"


@implementation MyCustomNavController {

    NSString *_savedTitle;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated withBackBtnTitle:(NSString *)title {
    _savedTitle = self.topViewController.title;

    self.topViewController.title = title;
    [super pushViewController:viewController animated:animated];
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {

    [self.viewControllers objectAtIndex:self.viewControllers.count - 2].title = _savedTitle;
    return [super popViewControllerAnimated:animated];
}

@end

天赋异禀 2024-08-12 03:51:00

self.navigationController.navigationBar.backItem.title = @"TEXT";

在 Swift 中:

self.navigationController?.navigationBar.backItem?.title = "TEXT"

self.navigationController.navigationBar.backItem.title = @"TEXT";

And in Swift:

self.navigationController?.navigationBar.backItem?.title = "TEXT"
橘香 2024-08-12 03:51:00
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] 
                                              initWithTitle:@"Log out" 
                                              style:UIBarButtonItemStyleDone 
                                              target:nil 
                                              action:nil] autorelease];

您可以将其放在父控制器代码中的任何位置,这样您就可以为不同的子视图提供不同的后退按钮。

self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] 
                                              initWithTitle:@"Log out" 
                                              style:UIBarButtonItemStyleDone 
                                              target:nil 
                                              action:nil] autorelease];

you can put it whereever you like in the code in the parrent controller, which allowes you to have differenct backbuttons for different child views.

流心雨 2024-08-12 03:51:00

大多数解决方案都取消了后退按钮(左箭头栏按钮)的原始样式,同时添加具有所需标题的常用按钮。
所以要保持原来的风格有两种方法:
第一:使用未记录的按钮样式(110 或类似的东西),我不喜欢这样做。但如果您愿意,您可以在 stackoverflow 上找到如何执行此操作。
第二:使用 Trenskow 的想法。我喜欢它,并且我使用它时做了一些改变。
我决定以以下方式保留原始标题,而不是覆盖 - (NSString*)title (这允许我使用笔尖的标题以及推送状态下的给定标题)。

- (void)viewDidLoad {
    [super viewDidLoad];
    static NSString * backButtonTitle=@"Back"; //or whatever u want

    if (![self.title isEqualToString:backButtonTitle]){

        UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
        customTitleView.text = self.title; // original title
        customTitleView.font = [UIFont boldSystemFontOfSize:20];
        customTitleView.backgroundColor = [UIColor clearColor];
        customTitleView.textColor = [UIColor whiteColor];
        customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
        customTitleView.shadowOffset = CGSizeMake(0, -1);

        [customTitleView sizeToFit];

        self.navigationItem.titleView = [customTitleView autorelease];
        self.title = backButtonTitle; 
    }
}

这个解决方案效果很好,而且看起来很原生。另外,如果在 viewDidLoad 方法中使用它,它会阻止执行超过 1 次。
我也尝试过 Jessedc 的解决方案,但看起来很糟糕。它会导致用户可见的标题栏从原始状态动态更改为后退按钮所需的状态并返回。

Most of solutions kills the original style of BackButton (The left arrowed bar button) while adding a usual button with desired title.
So to keep the original style there are 2 ways:
1st: To use undocumented button style (110 or something like that) which I prefer not to do. But if you want you could find how to do it here, on stackoverflow.
2nd: To use I the Trenskow's idea. I liked it and I use it a bit changed.
Instead of overriding - (NSString*)title I've decided to keep the original title in the following way (which allows me to use nib's titles as well as given title at push state btw).

- (void)viewDidLoad {
    [super viewDidLoad];
    static NSString * backButtonTitle=@"Back"; //or whatever u want

    if (![self.title isEqualToString:backButtonTitle]){

        UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
        customTitleView.text = self.title; // original title
        customTitleView.font = [UIFont boldSystemFontOfSize:20];
        customTitleView.backgroundColor = [UIColor clearColor];
        customTitleView.textColor = [UIColor whiteColor];
        customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
        customTitleView.shadowOffset = CGSizeMake(0, -1);

        [customTitleView sizeToFit];

        self.navigationItem.titleView = [customTitleView autorelease];
        self.title = backButtonTitle; 
    }
}

This solution works good and it looks native. Also if use it in the viewDidLoad method it prevents execution more then 1 time.
Also I've tried a Jessedc's solution but it looks bad. It causes visible to user title bar change on the fly from original to BackButton's desired and back.

深海蓝天 2024-08-12 03:51:00

这对我来说是以前发布的答案的“简化”版本。

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];

backButton.title = @"Go Back";

self.navigationItem.backBarButtonItem = backButton;

请记住将代码放入父视图控制器(例如,具有表视图或 UITableViewController 的视图)中,而不是子视图或详细视图(例如 UIViewController)中。

您可以轻松本地化后退按钮字符串,如下所示:

backButton.title = NSLocalizedString(@"Back Title", nil);

This works for me as a "simplified" version of previous posted answers.

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];

backButton.title = @"Go Back";

self.navigationItem.backBarButtonItem = backButton;

Remember to put the code inside the parent view controller (e.g. the view that has your table view or UITableViewController), not the child or detail view (e.g. UIViewController).

You can easily localize the back button string like this:

backButton.title = NSLocalizedString(@"Back Title", nil);
想挽留 2024-08-12 03:51:00

根据 UINavigationBar 的文档 backItem

在此处输入图像描述

如果最上面的导航项的 leftBarButtonItem 属性是
nil,导航栏显示后退按钮,其标题是派生的
来自此属性中的项目。

但是设置 backItem.backBarButtonItem 在第一次 viewWillAppear 中不起作用。设置 topItem.backBarButtonItem 仅在第一次 viewWillAppear 中有效。因为 navigationBar.topItem 仍然指向 previousViewController.navigationItem。在 viewWillLayoutSubviews 中,topItembackItem 已更新。因此,在第一次 viewWillAppear 后,我们应该设置 backItem.backBarButtonItem

答案:将 backBarButtonItem 设置为前一个 viewControllernavigationItem,无论何时何地在当前 viewController(顶部 viewController)中。您可以在 viewWillAppearviewDidLoad 中使用此代码。查看我的博文 iOS 设置导航栏返回按钮标题进行详细分析。

 NSArray *viewControllerArray = [self.navigationController viewControllers];
    // get index of the previous ViewContoller
    long previousIndex = [viewControllerArray indexOfObject:self] - 1;
    if (previousIndex >= 0) {
        UIViewController *previous = [viewControllerArray objectAtIndex:previousIndex];
        previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                         initWithTitle:backButtonTitle
                                                         style:UIBarButtonItemStylePlain
                                                         target:self
                                                         action:nil];
    }

According to document of UINavigationBar>backItem

enter image description here

If the leftBarButtonItem property of the topmost navigation item is
nil, the navigation bar displays a back button whose title is derived
from the item in this property.

But setting backItem.backBarButtonItem does not work in first time viewWillAppear. Setting the topItem.backBarButtonItem only works in first time viewWillAppear. Because navigationBar.topItem is still pointing to the previousViewController.navigationItem. In viewWillLayoutSubviews, the topItem and backItem are updated. So after 1st time viewWillAppear, we should set the backItem.backBarButtonItem.

ANSWER : Setting a backBarButtonItem to the navigationItem of the previous viewController no matter when and where in your current viewController (the top viewController). You can use this code in viewWillAppear or viewDidLoad. Check my blog post iOS Set Navigation Bar Back Button Title for detail analysis.

 NSArray *viewControllerArray = [self.navigationController viewControllers];
    // get index of the previous ViewContoller
    long previousIndex = [viewControllerArray indexOfObject:self] - 1;
    if (previousIndex >= 0) {
        UIViewController *previous = [viewControllerArray objectAtIndex:previousIndex];
        previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                         initWithTitle:backButtonTitle
                                                         style:UIBarButtonItemStylePlain
                                                         target:self
                                                         action:nil];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文