Richard Knop
Richard Knop

Reputation: 83725

Cannot add buttons to table view toolbar

So I have a root view with table view. I display the toolbar like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.navigationController.toolbarHidden = NO;
}

And I implement the setToolbarItems method:

- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated
{
    UIBarButtonItem *buttonItem;
    buttonItem = [[UIBarButtonItem alloc] initWithTitle:@"Hello" style:UIBarButtonItemStyleDone target:self action:@selector(goBack:)];
    self.navigationController.toolbarItems = [ NSArray arrayWithObject: buttonItem ];
}

The result is an empty tolbar. Why?

enter image description here

Upvotes: 0

Views: 545

Answers (2)

Benjamin Cox
Benjamin Cox

Reputation: 6120

But who calls your implementation of setToolbarItems?

You're supposed to call setToolbarItems on your own view, not re-implement it. Then, the NavigationController will find them in the instance variable and render them.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.navigationController.toolbarHidden = NO;

    UIBarButtonItem *buttonItem;
    buttonItem = [[UIBarButtonItem alloc] initWithTitle:@"Hello" style:UIBarButtonItemStyleDone target:self action:@selector(goBack:)];
    [self setToolbarItems: [ NSArray arrayWithObject: buttonItem ]];
} 

Upvotes: 1

CodaFi
CodaFi

Reputation: 43330

From the docs:

toolbarItems The toolbar items associated with the view controller.

@property(nonatomic, retain) NSArray *toolbarItems Discussion This property contains an array of UIBarButtonItem objects and works in >conjunction with a UINavigationController object. If this view controller is >embedded inside a navigation controller interface, and the navigation >controller displays a toolbar, this property identifies the items to display in >that toolbar.

You can set the value of this property explicitly or use the >setToolbarItems:animated: method to animate changes to the visible set of >toolbar items.

In other words, try accessing it through the actual view controller, not it's navigation controller like so:

self.toolbarItems = [ NSArray arrayWithObject: buttonItem ];

Upvotes: 1

Related Questions