views:

4493

answers:

9

Currently the left bar button default value is the title of the view that loaded the current one, in other words the view to be shown when the button is pressed (back button).

I want to change the text shown on the button to something else.

I tried putting the following line of code in the view controller's viewDidLoad method but it doesn't seem to work.

self.navigationItem.leftBarButtonItem.title = @"Log Out";

What should I do?

Thanks.

+25  A: 

I edited this to provide the definitive answer:

This should be placed in the in the method that calls the ViewController titled "NewTitle". Right before the push or popViewController statement.

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"NewTitle" style: UIBarButtonItemStyleBordered target: nil action: nil];

[[self navigationItem] setBackBarButtonItem: newBackButton];

[newBackButton release];
Jordan
When in the child view controller, calling this from the child view controller will not work. You have to call this from the parent view controller, while within the child view controller.
Alex Reynolds
Alex, the answer was edited. It will work.
Jordan
I tested your answer by putting it into the child view controller's `-viewDidAppear:` method and it actually does *not* work. I think this is because you are calling this in the child view controller, and the `-setBackBarButtonItem:` method only manipulates the navigation bar when in the child of the the current view controller. In other words, your code only changes the *child's child* back button.
Alex Reynolds
Edited to add a little more clarity on where to put the code.
Jordan
EXCELLENT!!!!!!!! Just what I wanted, and it makes sense to me too!!!Thank you thank you thank you.
Dale
Then make this the answer because the one the one you chose is technically incorrect.
Jordan
Or in one line: self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"") style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease]; Thanks!
Dimitris
To change the back button for my third level view controller (second push) I had to use the following line instead of the above answer: [[self.parentViewController navigationItem] setBackBarButtonItem: newBackButton]; Am I doing something wrong?
JonB
worked perfectly, thanks!
Bogatyr
A: 

You actually want to be using the "backBarButtonItem" property.

And it behaves strangely. If you have ViewController "A", and ViewController "B", then when you navigate to "B", the back button says "A". If you want it to say "Previous" instead, you don't put the code inside B's ViewController. Instead, you put it inside A's controller. The self.navigationItem.backBarButtonItem refers to the back button that would take you to this view controller (self).

So inside your "A" controller, you'd put:

self.navigationItem.backBarButtonItem.title = @"Previous";

It's weird.

Dave DeLong
Didn't work for me. :-(
Dale
+1  A: 

Here's another way to do it.

In your parent view controller, implement the following method:

- (void) setBackBarButtonItemTitle:(NSString *)newTitle {
  self.navigationItem.backBarButtonItem.title = newTitle;
}

In your child view controller, when you want to change the title, this will work:

NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];

I was never able to get the parentViewController property to work:

[(ParentViewController *)(self.navigationController.parentViewController) setBackBarButtonItemTitle:@"New Title"];

I don't know if that's a bug or I'm not using it properly. But grabbing the second-to-last view controller in the viewControllers array points to the parent view controller, and I can call parent methods correctly with that reference.

Alex Reynolds
+1  A: 

Ok, here is the way. If you have a view controller "first" and you navigate another view controller "second" by pushing a button or etc. you need to do some work. First you need to create a BarButtonItem in "second" view controller's ViewDidLoad method like this;

    UIBarButtonItem *LOButton = [[UIBarButtonItem alloc]
              initWithTitle:@"Log Out" 
              style:UIBarButtonItemStyleBordered
              target:self
              action:@selector(lOut:)];
    self.navigationItem.leftBarButtonItem = LOButton;
    [LOButton release];

After you do that, you need to write to code for "lOut" action in the same .m file like this;

-(IBAction)lOut:(id)sender

{

    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController pushViewController:self.navigationController.parentViewController animated:YES];

}

It's all.

EEE
A `leftBarButtonItem` does not have the appearance of a `backBarButtonItem`, if that is important in the application design.
Alex Reynolds
Of the solutions posted (thank you very much) this one seems to work, albeit as Alex stated above, the left bar button doesn't look like a back bar item. How can I make it look like a back bar item???
Dale
A: 

I got the solution that puts a left bar button on the navigation bar to say log out. How do I make it be a back bar button???

Dale
If you want the behavior to go back to the previous ViewController, see my answer above (Which you should mark as the answer to the question).If you want the behavior to do something else other than go back to the previous ViewController, you can add a navigationItem button as per some of the post here.
Jordan
I assume I do that by clicking on the check box Jordan? If so, I just did, and I appreciate the help!
Dale
+1  A: 

Maybe I'm being over simplistic but From Apple's documentation the wording is:

If a custom bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the stack.

The solution marked correct above sets a default button item from the parent controller. It's the right answer, but what I'm doing is simply changing the self.title property right before pushing the new controller onto the NavigationController stack.

This seems to update the back button's title on the next controller.

As long as you set the title back to what it should be in viewWillDisplay I can't see this causing any problems...

thoughts?

Jessedc
I confess to doing this as well :)
willcodejavaforfood
A: 

This last post from Jessedc is simple and works a treat to control the title of BACK navigation item, cheers, very simple.

Neill