xcode 如何在 UINavigationBar 上设置后退按钮的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2197698/
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
How to set the text of a back button on a UINavigationBar?
提问by RexOnRoids
Possible Duplicate:
How do I change the title of the “back” button on a Navigation Bar
可能的重复:
如何更改导航栏上“后退”按钮的标题
The Situation:
I have a UIViewController that is governed by a navigation controller. I am able to set the titleof the navigation bar covering the UIViewController by simply calling self.title = @"title"
. But, I want to be able to set the back button textof the navigation bar, too (for different languages n' such..)
情况:
我有一个由导航控制器管理的 UIViewController。我可以通过简单地调用来设置覆盖 UIViewController 的导航栏的标题self.title = @"title"
。但是,我也希望能够设置导航栏的后退按钮文本(对于不同的语言,例如..)
The Problem:
I don't know how to set the back button's text.
问题:
我不知道如何设置后退按钮的文本。
The Question:
How do I set the back button of the UINavigation bar (programmatically not in IB)?
问题:
如何设置 UINavigation 栏的后退按钮(以编程方式不在 IB 中)?
回答by Boon
The setTitle way - though may seem to work, is incorrect. If you pay attention closely, you will notice that the navigation item changes to the new title while the new view is animated into view via pushViewController. The fact that you have to restore your title in viewWillAppear is kludgy, which further suggests its hack nature.
setTitle 方式 - 虽然看起来可行,但不正确。如果您仔细观察,您会注意到导航项更改为新标题,同时新视图通过 pushViewController 动画进入视图。您必须在 viewWillAppear 中恢复标题这一事实很笨拙,这进一步表明了它的黑客性质。
The correct way is to do the following before your pushViewController:
正确的方法是在 pushViewController 之前执行以下操作:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle: @"Back Button Text" style: UIBarButtonItemStyleBordered target: nil action: nil]; [self.navigationItem setBackBarButtonItem: backButton]; [backButton release];
回答by Jlam
The best and easiest way according to apple documentation to set the view controller's title to what the back should say is this -
根据苹果文档将视图控制器的标题设置为后面应该说的最好和最简单的方法是 -
Example:
例子:
If viewController1
is under viewController2
(and the back button
on viewController2
is going to viewController1
)
如果viewController1
是在viewController2
(与back button
上viewController2
即将viewController1
)
viewController1.title = @"something awesome";
the back button
on viewController2
will show "something awesome"
在back button
上viewController2
会显示“东西真棒”