NavigationController UINavigationBar Back Button

2019-05-30 03:55发布

问题:

I'm trying to change the text of the back button from within Interface Builder, but when I select the Navigation Bar and go into the Attributes Inspector and set the Back Button text to "Close" it still shows up with the title of the previous view.

How do I get this to work?

回答1:

The back button will always show the previous UIViewController title or backBarButtonItem defined.

So if you have "view1" and move to "view2" you need to set the backButton in "view1" so it's displayed correctly while "view2" is presented.
(In other word "view1" is responsible to provide what should be displayed in a back button that point to it)
In this way if "view1" is follow by any views all those will have the correct back button.



回答2:

Try this in -(void)viewDidload, as this method is fired after the nib has loaded:

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


回答3:

A quick and dirty trick is to set the "title" to what name you want in the button in the willDisappear override in the view controller. Make sure to set the "title" back to the correct name on the willAppear override view controller. One drawback with this technique is that on slower units you can see it change. On iPhone 4 and later it's hardly noticeable.

Hope this helps.