user393553
user393553

Reputation:

How to add bar buttons to UINavigation bar

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.

Upvotes: 4

Views: 8615

Answers (1)

Jasarien
Jasarien

Reputation: 58448

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];

Upvotes: 11

Related Questions