views:

870

answers:

5

I have a UINavigationController with a UITableViewController in it. I want to show a ToolBar on the bottom with UIBarButtonItem's. The ToolBar is showing up, but the buttons won't appear. Anyone knows why?

  - (void)viewDidLoad {
        [super viewDidLoad];
     [[self navigationItem] setTitle:@"Selections List"];
     [[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addProjectSearch:)] autorelease]];
        [[self navigationItem] setLeftBarButtonItem:[self editButtonItem]];
     [[super tableView] setDataSource: self];
     [[super tableView] setDelegate: self];

     //Toolbar 
     UIBarButtonItem * logoutButton = [[[UIBarButtonItem alloc] initWithTitle:@"Log out" style:UIBarButtonItemStylePlain target:self action:@selector(logOut:)]autorelease];
     NSMutableArray * arr = [NSMutableArray arrayWithObjects:logoutButton, nil];
     [[self navigationController] setToolbarHidden: NO animated:YES];
     [[self navigationController] setToolbarItems:arr animated:YES]; 
    }
A: 

Maybe you can use interface builder to avoid this, however it will be slower

giftederic
But where do I add that UIToolBar to. I couldn't add it to my UITableViewController
Olivier de Jonge
A: 

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html

"The navigation controller object now manages an optional toolbar in its view hierarchy. When displayed, this toolbar obtains its current set of items from the toolbarItems property of the active view controller."

Have you tried subclassing UITableViewController for your tableview and setting up with the appropriate toolbarItems property?

xyzzycoder
I'm subclassing the UITableViewController. What you mean with "appropriate toolbarsItem"? I've also tried it with: [self setToolbarItems:arr animated:YES]; But that still doesn't work.
Olivier de Jonge
I've down voted this answer because it sounds to me like Olivier has read the documentation and is still having a problem - which is why he's come here for advice. Also, while it didn't help Olivier, Tom's answer solved my problem.
Matt
+3  A: 

Replace this line:

[[self navigationController] setToolbarItems:arr animated:YES];

with this:

[self setToolbarItems:arr animated:YES];

In general, you should set toolbarItems on each individual view controller that you push, and not on your UINavigationController itself.

Tom
Now why doesn't this still not work for me, it seemed to be the perfect answer...
Olivier de Jonge
+1  A: 

I found out in the documentation of Apple there is small paragraph explaining the UIToolBar. In this paragraph there is a very tiny sentence stating: "[..] When displayed, this toolbar obtains its current set of items from the toolbarItems property of the active view controller [..]" But they don't explain that view first has to be active to obtain these buttons. So that means that the UIToolBar is ready to retrieve it's Buttons on viewDidAppear and NOT on viewDidLoad message.

- (void)viewDidAppear:(BOOL)animated {
    [[self tableView] reloadData];

    [[self navigationController] setToolbarHidden: NO animated:YES];    
    UIBarButtonItem * logoutButton = [[[UIBarButtonItem alloc] initWithTitle:@"Log out" style:UIBarButtonItemStylePlain target:self action:@selector(logOut:)]autorelease];
    NSMutableArray * arr = [NSMutableArray arrayWithObjects:logoutButton, nil];
    [self setToolbarItems:arr animated:YES];

    [super viewDidAppear:animated];
}
Olivier de Jonge
A: 

I've made a view controller, which is a subclass of UITableViewController, and I've got the toolbar working by doing the following:

In viewDidLoad:

self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;

NSArray* toolbarItems = [NSArray arrayWithObjects: button1,
                                                   button2,
                                                   button3,
                                                   nil];

[self setToolbarItems:toolbarItems animated:NO];

Then, because I want the toolbar only on this screen, I added this to viewWillAppear:

[self.navigationController setToolbarHidden:NO animated:YES];

And finally, I hide the toolbar again in viewWillDisappear:

[self.navigationController setToolbarHidden:YES animated:YES];

This works for me with "text" buttons, built in icons and custom icons.

Matt