views:

2469

answers:

5

Hi,

By default the back button uses as a text on it a title of a viewcontroller. Can I change text on the back button without changing a title of a view controller? I need this because I have a view controller which title is too long to display and in this case I would like to display just "Back" as a caption for back button.

I tried the following which didn't work:

self.navigationItem.leftBarButtonItem.title = @"Back";

Thanks.

+13  A: 

Try

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

I found that by looking at the backBarButtonItem docs in Apple's docs for UINavigationController.

Marc W
Sorry, I don't have a chance to test this right now, but does this apply the left-pointed shape of the built-in Back buttons, or does it keep the UIBarButtonItem's default rectangular shape?
Marco
I'm actually not sure. I assume that it would just keep the back button shape automatically since you're setting the backButton property, but you'd have to test it to find out. I'm curious, too.
Marc W
Yes, it worked! It yes changes the title, and yes, saves the shape.Thanks for helping.
Ilya
Note: the back button is retained twice [1x from alloc, 1x from the property's retain]. However, if you don't change the back button, it's effectively harmless.
Kelvin
Yep, works for me too :D Thanks!
Jasarien
+7  A: 

Marc W's approach worked great once I figured out which controller to apply it to: the one being re-titled, not the one on top. So if this is the navigation stack:

(bottom)  ControllerA -> ControllerB  (top)

...and you want to give a shorter title for ControllerA in the back-button displayed when ControllerB is on top, you apply the property change to ControllerA.

So it's more in the context of self.title, not like the other left/right-bar-button setters.

Marco
+1  A: 

It doesn't work for me. The pointed shape is preserved, but the title of the navigation controller is displayed instead of the one specified in initWithTitle:. Using a normal Button displays as a Rectangle, but the custom title is preserved.

I'm using SDK 3.x

Nicolás
+2  A: 

Thanks Marco... that helped me...

Here is what i did.

If you are using a tableView to navigate to different views... put the code:

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

In your didSelectRowAtIndexPath method... of the first Controller... Controller A.

When you navigate to Controller B the button will have the title "Back".

Ben Call
A: 

The back button pulls its text from the title of the parent view controller.

In the parent view controller (the view controller that appears when you tap the back button), set its own title as the desired text on the back button.

For example, let's say we have a RootViewController class. When we click a cell in its table view, we push an instance of SecondViewController. We want the back button of the SecondViewController instance to read, "Home."

in the viewDidLoad method of RootViewController.m:

self.title = @"Home";

in the viewDidLoad method of SecondViewController.m:

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

If you want your back button to read, "Back," set the title of the parent view controller to @"Back";

Rose Perrone