如何设置 UINavigationBar 上后退按钮的文本?

发布于 2024-08-19 21:18:34 字数 520 浏览 6 评论 0原文

可能的重复:
如何更改导航栏上“后退”按钮的标题

情况:
我有一个由导航控制器控制的 UIViewController。我可以通过简单地调用 self.title = @"title" 来设置覆盖 UIViewController 的导航栏的标题。但是,我也希望能够设置导航栏的后退按钮文本(对于不同的语言等......)

问题:
我不知道如何设置后退按钮的文本。

问题:
如何设置 UINavigation 栏的后退按钮(不在 IB 中以编程方式设置)?

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 title of the navigation bar covering the UIViewController by simply calling self.title = @"title". But, I want to be able to set the back button text of the navigation bar, too (for different languages n' such..)

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)?

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

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

发布评论

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

评论(2

近箐 2024-08-26 21:18:34

setTitle 方法虽然看起来可行,但却是不正确的。如果您仔细观察,您会注意到导航项更改为新标题,同时新视图通过 PushViewController 动画化到视图中。事实上,你必须在 viewWillAppear 中恢复你的标题是很糟糕的,这进一步表明了它的黑客性质。

正确的方法是在pushViewController之前执行以下操作:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
                                initWithTitle: @"Back Button Text" 
                                style: UIBarButtonItemStyleBordered
                                target: nil action: nil];

[self.navigationItem setBackBarButtonItem: backButton];
[backButton release];

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.

The correct way is to do the following before your pushViewController:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
                                initWithTitle: @"Back Button Text" 
                                style: UIBarButtonItemStyleBordered
                                target: nil action: nil];

[self.navigationItem setBackBarButtonItem: backButton];
[backButton release];

话少心凉 2024-08-26 21:18:34

根据苹果文档,将视图控制器的标题设置为背面应显示的内容的最佳和最简单的方法是这样的 -

示例:

如果 viewController1 位于 viewController2(并且 viewController2 上的后退按钮将转到 viewController1

viewController1.title = @"something awesome";

viewController2 上的后退按钮 将显示“很棒的东西”

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.title = @"something awesome";

the back button on viewController2 will show "something awesome"

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