Ios – How to add UIBarButtonItem in UIToolBar in code

iosobjective cuibarbuttonitemxcode

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.

Best Answer

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;