PiccoloBuddha
PiccoloBuddha

Reputation: 31

Back button on UINavigationController

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?

Upvotes: 1

Views: 5372

Answers (5)

Atef
Atef

Reputation: 2902

Apple Document says:

When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item.

So If your navigation item is the top of the Stack (as we are talking here) you can't add the back button to the navigation controller, simply because no place he can navigate back to it because it's the top item in the stack.

Updated Answer : After I searched I found work a round to make a back button in your root view controller in Navigation controller in these link

It's very simple :)

[self.navigationItem setHidesBackButton:YES animated:YES];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStyleBordered target:self action:@selector(initializeStuff)];

self.navigationItem.leftBarButtonItem = backButton;

Upvotes: 0

Pavan
Pavan

Reputation: 18508

Of course 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.

Upvotes: 0

johndpope
johndpope

Reputation: 5249

Usually this works out of the box, but sometimes with modal views / action sheets you may need this. Just before you instantiate your viewcontroller and push it onto navigationcontroller stack, try

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Back" style: UIBarButtonItemStyleBordered target: nil action: nil];
    [[self navigationItem] setBackBarButtonItem: newBackButton];
    [newBackButton release];

    DetailViewController *detailVC = [[DetailViewController alloc]init];
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];

Upvotes: 1

Pavan
Pavan

Reputation: 18508

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

Upvotes: 0

David Gelhar
David Gelhar

Reputation: 27900

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

Upvotes: 0

Related Questions