views:

159

answers:

1

Hey, I am trying to add bar button items to my UINavigationBar (nav bar) but I have found out that bar button items are not properties of navigation bar and hence can't be accessed directly like:

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 

    style:UIBarButtonSystemItemDone target:nil action:nil];

    navBar.rightBarButtonItem =rightButton;

I am creating my navigation bar programmatically.

Apparently, there is something called UINavigation Item that has to be dealt with to add bar buttons. Can anyone tell me how to go about this? How to create this UINavigation item programmatically and add this to my navBar and then add the bar buttons to it.

+1  A: 

UINavigationBars shouldn't be used on their own, outside of a UINavigationController.

When a navigation controller is used, each view controller on the navigation stack has a UINavigationItem. It is this UINavigationItem that you can add bar button items to.

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                           style:UIBarButtonSystemItemDone
                                                                          target:nil action:nil] autorelease];
Jasarien
Can I create a UIToolBar with right and left bar buttons? When one adds the toolbar and bar buttons from the Interface Builder, the button sort of line up from left to right. How can one insert space between them so as to make them look like right and left bar buttons of the UINavigation bar?
Bogus Boy
Figured it out. Got it from the UICatalog application available on the iPhone Developer guide. Yeppie!!
Bogus Boy