Как добавить UIBarButtonItem в UIToolBar в коде - PullRequest
10 голосов
/ 22 марта 2011

У меня есть стандартный UIBarButtonItem

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

Как добавить ее в UIToolBar? Я пробовал

    self.toolbarItems = [NSArray arrayWithObject:share];

Но это не работает. Нужна ваша помощь.

Ответы [ 4 ]

11 голосов
/ 22 марта 2011

Можете ли вы быть более конкретным, чем "это не работает"?

Если вы пытаетесь добавить элемент на панель инструментов, в которой уже есть элементы, вам нужно изменить массив элементов:

NSMutableArray *newItems = [self.toolbarItems mutableCopy];
[newItems addObject:share];
self.toolbarItems = newItems;
9 голосов
/ 22 марта 2011

Убедитесь, что вы сделали панель инструментов IBOutlet или добавили панель инструментов программно

IBOutlet UIToolbar *toolBar;

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

toolBar.items = [NSArray arrayWithObjects:infoButtonItem, nil];
1 голос
/ 22 марта 2011

Убедитесь, что панель инструментов не скрыта; Вы можете попробовать добавить следующее в метод viewWillAppear:animated: вашего контроллера представления:

[self.navigationController setToolbarHidden:NO animated:YES];
0 голосов
/ 22 марта 2011

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

...