Problem adding UIBarButtonItems to a ToolBar
Asked Answered
E

5

12

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]; 
    }
Entrap answered 19/3, 2010 at 6:5 Comment(2)
I'm having this exact problem, and none of the proposed solutions are working for me. Perhaps someone can comment on what things can go wrong and what prevents working. Specifically: Tom's set-on-VC, Olivier's in-viewDidAppear didn't work. FWIW, My toolbar appears, but with no buttons. (Yes I called setToolbarItems: !)Stettin
EDIT/Follow-up: My mistake. Re-reading Tom's & Olivier's answers and applying their recommendations more carefully got me up & running. +1 to both.Stettin
E
15

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];
}
Entrap answered 19/3, 2010 at 10:0 Comment(2)
Yes! setToolbarHidden, setToolbarItems, and [super viewDidAppear:] must be called.Pedometer
can we set the positions of UIBarButtonItemPhysique
S
48

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.

Spirula answered 19/3, 2010 at 7:42 Comment(1)
Now why doesn't this still not work for me, it seemed to be the perfect answer...Entrap
E
15

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];
}
Entrap answered 19/3, 2010 at 10:0 Comment(2)
Yes! setToolbarHidden, setToolbarItems, and [super viewDidAppear:] must be called.Pedometer
can we set the positions of UIBarButtonItemPhysique
H
0

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

Highpriced answered 19/3, 2010 at 6:29 Comment(1)
But where do I add that UIToolBar to. I couldn't add it to my UITableViewControllerEntrap
L
0

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?

Lima answered 19/3, 2010 at 7:13 Comment(2)
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.Entrap
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.Montanez
M
0

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.

Montanez answered 12/6, 2010 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.