Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add UIBarButtonItem in UIToolBar in code

I have standart UIBarButtonItem

UIBarButtonItem *share = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(share:)];

How to add her to UIToolBar? I've tried

    self.toolbarItems = [NSArray arrayWithObject:share];

But it doesn't work. Need your help.

like image 319
Tunyk Pavel Avatar asked Mar 21 '11 21:03

Tunyk Pavel


2 Answers

Can you be more specific than "it doesn't work"?

If you're trying to add an item to a toolbar that already has items, you'll need to modify the array of items:

NSMutableArray *newItems = [self.toolbarItems mutableCopy];
[newItems addObject:share];
self.toolbarItems = newItems;
like image 171
Caleb Avatar answered Sep 23 '22 16:09

Caleb


Make sure you have make a toolbar either an IBOutlet or added toolbar programatically

IBOutlet UIToolbar *toolBar;

UIBarButtonItem *infoButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"info" style:UIBarButtonItemStyleBordered  target:self action:@selector(infoButtonClicked)];

toolBar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
like image 34
Gypsa Avatar answered Sep 21 '22 16:09

Gypsa