How to set the text of a back button on a UINavigationBar? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-11-26 09:34:33

问题


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


回答1:


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




回答2:


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"



来源:https://stackoverflow.com/questions/2197698/how-to-set-the-text-of-a-back-button-on-a-uinavigationbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!