1
votes

my application has a UIViewController subclass which is being managed by a UINavigationController.

In the viewDidLoad of my UIViewController subclass, I was attempting to add a UIBarButtonItem to the toolbar like this:

settingsButton = [[UIBarButtonItem alloc] initWithTitle:@"Settings"
   style:UIBarButtonItemStylePlain target:self action:@selector(viewSettings:)];
[self setToolbarItems:[NSArray arrayWithObject:settingsButton]];

this wasn't working out for me, so after some googling around, I tried this:

[[self navigationItem] setRightBarButtonItem:settingsButton];

which worked out fine. from reading the UIViewController documentation, I'm still confused about why setToolbarItems wasn't working. I verified in the debugger that the button was in the toolbarItems array in the viewDidAppear method. the button itself just wasn't appearing on my toolbar.

so, my question is, why didn't setToolbarItems work for me in the first code snippet?

I don't have the toolbar configured in my xib for this view controller at all, if that makes a difference.

4

4 Answers

5
votes

Yes that make the difference.Whenever you see a bar on view by default for navigation based apps that is not a toolBar actually that is , navigation bar.so you can add item by referencing self.navigationItem.

2
votes
  1. [self setToolbarItems:[NSArray arrayWithObject:settingsButton]]; essentially populates the navigation controller's bottom toolbar - not the Left and Right top bar buttons.

  2. The bottom toolbar is, by default, not displayed. To display it you must call [self.navigationController setToolbarHidden:NO]

  3. Below is the relevant documentation - UINavigationController Class Reference toolbar:

The custom toolbar associated with the navigation controller. (read-only)

@property(nonatomic,readonly) UIToolbar *toolbar Discussion This property contains a reference to the built-in toolbar managed by the navigation controller. Access to this toolbar is provided solely for clients that want to present an action sheet from the toolbar. You should not modify the UIToolbar object directly.

Management of this toolbar’s contents is done through the custom view controllers associated with this navigation controller. For each view controller on the navigation stack, you can assign a custom set of toolbar items using the setToolbarItems:animated: method of UIViewController.

The visibility of this toolbar is controlled by the toolbarHidden property. The toolbar also obeys the hidesBottomBarWhenPushed property of the currently visible view controller and hides and shows itself automatically as needed.

0
votes

try to use

[toolbar setItems:[NSArray arrayWithObject:settingsButton] animated:YES];

instad of :

[self setToolbarItems:[NSArray arrayWithObject:settingsButton]];

shani

0
votes

On ipad apps, you've got to set toolbar items to the "topViewController" (yes this is counter-intuitive).

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:catView];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"item 1" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"item 2" style:UIBarButtonItemStylePlain target:nil action:nil];
[nav setToolbarHidden:NO animated:YES];
// WRONG: [nav setToolbarItems:[NSArray arrayWithObjects:addButton, nil]];
// CORRECT (for ipad apps):
[nav.topViewController setToolbarItems:[NSArray arrayWithObjects:item1, item2, nil] animated:NO];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:nav];