1

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?

PiccoloBuddha
  • 31
  • 2
  • 5

5 Answers5

1

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];
johndpope
  • 5,035
  • 2
  • 41
  • 43
0

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;
Community
  • 1
  • 1
Atef
  • 2,872
  • 1
  • 36
  • 32
0

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

Community
  • 1
  • 1
David Gelhar
  • 27,873
  • 3
  • 67
  • 84
0

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.

Pavan
  • 17,840
  • 8
  • 59
  • 100
  • I tried as you wrote but nothing appear. Do I have to set a text bar button before use your code? – PiccoloBuddha Sep 28 '10 at 12:29
  • guys you need to make sure you lot set the delegate in the did load method and also make sure the button is instantiated in the same method in this case the leftBarButtonItem, then you can start doing things like what ive written in the answer – Pavan Jun 13 '11 at 13:23
  • or if not navigationItem... try navigationController, either way, the above is how you go about setting a backbutton. – Pavan Jun 13 '11 at 13:24
0

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
  • 17,840
  • 8
  • 59
  • 100