views:

41

answers:

2

Hi, I have a UIViewController with a UITableView in it, and also added a UINavigationBar. How can I add and "edit" button and a "+" button in that bar programmatically? (I have tried using IB, but the title is always replaced, and not other items are added) I am not using a UINavigationController. is my UIViewController standing alone.

This is what I have tried without success:

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered 
                       target:nil
action:nil];
UINavigationItem *editItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
[editItem setLeftBarButtonItem:barButton animated:YES];
[navigationBar setItems:[NSArray arrayWithObject:editItem] animated:YES];
+1  A: 

Your UIViewController has a navigationItem property. You can set the left and right bar button items with self.navigationItem.leftBarButtonItem = ... and self.navigationItem.rightBarButtonItem = ...

Edit:

OK, I assume you have a reference to your UINavigationBar? Then I guess you'd add a single UINavigationItem to it:

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"A Title"];
theNavigationBar.items = [NSArray arrayWithObject:item];
[item release]; // or keep this as an instance variable

and then set that item's left and right buttons:

theNavigationBar.topItem.leftBarButtonItem = ...;
theNavigationBar.topItem.rightBarButtonItem = ...;

I haven't tried this, but I think it should work.

Thomas Müller
This is the same approach as vfn right? I didn't work. navigationItem is intended to be used when using UINavigationController, which is not my case.
nacho4d
Yes, it's the same approach. I edited my answer and added another possible solution.
Thomas Müller
Thanks, it worked!
nacho4d
A: 
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(theEditMethod:)];      
[viewController.navigationItem setLeftBarButtonItem:leftBarButton animated:NO];
[leftBarButton release];

UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(theAddMethod:)];       
[viewController.navigationItem setLeftBarButtonItem:rightBarButton animated:NO];
[rightBarButton release];
vfn
I tried this inside of viewDidLoad method of the controller (and replaced viewController for self) but not working. ;(. navigationItem is intended to be used when using UINavigationController, which is not my case.
nacho4d
Why don't you switch to use a navigation controller and set your view controller as root of it. It will make you life easier!
vfn
Because, first, I am not navigating. And second, I want to customize its width, something is not possible with UINavigationController. Actually I am working on an alternative way of navigating so the goal is not to use UINavigationController.
nacho4d
Well, remember that a UINavigationController is a subclass of a UIViewController that has a UINavigationBar added to it's UIView. If you want to create your own way to navigate, you can do it using a UINavigationController, just don't use push and pop view controllers. :). If you don't want to use the default navigation structure, I woul recommend to you to use so a UIToolbar instead of a UINavigationBar.
vfn