4

I have been trying to create a navigation bar back button.

Here is my code :-

UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:nil]autorelease];
self.navigationItem.rightBarButtonItem = barButton;

But it isn't displaying anything on navigation bar.

I am using UIViewController, not an UINavigationController.

Is UINavigationController is the only way to achieve this?

Any help would be really appreciated.

Any link to a good tutorial will be great.

Thanks.

N West
  • 6,768
  • 25
  • 40
Varundroid
  • 9,135
  • 14
  • 63
  • 93

6 Answers6

5

Without the viewcontroller having a navigation controller (i.e viewController.navigationController != nil) you cannot add it in this manner.

One thing you can do is if it is being created by the nib is to just drag a bar button item into a navigation bar and link it via IBAction.

Kaiser
  • 688
  • 4
  • 12
  • 1
    I have tried that but wasn't able to drop it. I dragged it and as soon as i drop it on NavigationBar it disappear but i can see it in my Placeholder. Really confused what is this all about. I can't see it in my view but if i check placeholder, it shows that its right there. – Varundroid Aug 31 '11 at 18:52
  • 2
    If you are trying to add the back button that looks like an arrow, some help may be found here: http://stackoverflow.com/questions/227078/creating-a-left-arrow-button-like-uinavigationbars-back-style-on-a-uitoolbar – Kaiser Aug 31 '11 at 18:59
  • That looks great. thank for the link man. let me try it. Cheers :) – Varundroid Aug 31 '11 at 19:03
5

I'd recommend pushing this view controller out of a nvigationcontroller - you will get all those things for free:

UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:vc];
TommyG
  • 4,145
  • 10
  • 42
  • 66
  • Thanks man, I know tony that there is always an easy way but right now i am learning iOS development and i want to learn hard way as well. I am just messing with iPhone SDK to know how it really works. ;) – Varundroid Aug 31 '11 at 19:01
  • This is not an easy way - its the more robust way in my mind, cause you set your view as a nav controller and get everything you need. good luck learning! – TommyG Aug 31 '11 at 19:03
  • Thanks man i didn't know that. I will keep it in my mind as i am newbie so every piece of knowledge is important for me. – Varundroid Aug 31 '11 at 19:06
  • cool man. made a small correction - had a typo. good luck again! – TommyG Aug 31 '11 at 19:09
  • 1
    I tried your code and its fantastic +1 from me. Thanks man :) – Varundroid Aug 31 '11 at 19:38
3

If you have your view controller being created from a NIB file in Interface Builder, and the view controller's design surface has a navigation bar on it, the easiest way to do this would be to drag a bar button item from the objects inspector right onto the navigation bar's right side. You would then create an IBAction in your header and implementation that you would hook up to.

BP.
  • 10,033
  • 4
  • 34
  • 53
  • I have done it but it will add a rect bar button on my NavigationBar but i need that arrow one which we call navigation item. Any idea how to do it? – Varundroid Aug 31 '11 at 18:54
1

I achieve this inserting the button directly in the UINavigationBar:

[yourUINavBar insertSubview:yourButton atIndex:1];
flopes
  • 1,345
  • 1
  • 14
  • 21
0

UIViewController's -navigationItem method does only work together with UINavigationController or other UIViewController containments. You will have to get access to the UINavigationBar and set the item directly.

Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60
0

If you want to have a navigation bar, and have it work as you expect, create a UINavigationController using your UIViewController as the root view controller. Use that UINavigationController where you are using your UIViewController now.

Check out UINavigationController at developer.apple.com for more details.

marty
  • 486
  • 5
  • 18
Mike Hay
  • 2,828
  • 21
  • 26