views:

127

answers:

3

I know that it could seem strange but i need to add a back button on the navigation Bar of the first navigationController's view. I tried like this:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Foo" style:UIBarButtonItemStyleBordered target:self                                                          action:@selector(foo:)]; 
self.navigationItem.backBarButtonItem=backButton;

if instead of backBarButtonItem i write leftBarButtonItem the button is showed. My problem is that i need an arrow button as the normal back button. Is this possible?

A: 

I don't think you can do that on the first NavigationController view, because you need to set the backBarButtonItem property in the parent controller, before the child controller is pushed. Also, according the to the Apple docs, the target & action of the backBarButtonItem must be nil.

This question about creating a left-arrow button on a UIToolbar may give you some ideas of how you could work around this using a custom image (for the leftBarButtonItem).

David Gelhar
A: 

ofcourse you can do this. You just need to change the leftBarButtonItem's title to back then you will get a nice left arrow button with the title back. Then you just change the selector to actually perform a method when the button is clicked. so @selector(foo:)

here some code on how to achieve the above:

self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;

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

self.navigationItem.leftBarButtonItem.target = self;

self.navigationItem.leftBarButtonItem.action = @selector(endTextEnteringButtonAction:);

let me know if that helps. Pk

Pavan
I tried as you wrote but nothing appear. Do I have to set a text bar button before use your code?
PiccoloBuddha
A: 

or you could also do the following - I prefer this method. I got this from a different post.

Use following psd that I derived from http://www.teehanlax.com/blog/?p=447

http://www.chrisandtennille.com/pictures/backbutton.psd

Then I just create a custom UIView that I use in the customView property of the toolbar item.

Works well for me.

Hope that helps a little

Pavan