0

I need to add buttons to the UIToolbar of a UINavigationController, which is also my root. I want the UIToolbar to appear when a specific UIViewController is shown. Therefore, I placed this code in my viewDidLoad method of my UIViewController subclass:

UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething)];
item.width = 300;
item.tintColor = [UIColor whiteColor];
UIBarButtonItem* item2 = [[UIBarButtonItem alloc] initWithTitle:@"Title2" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething)];

NSMutableArray* theItems = [self.navigationController.toolbar.items mutableCopy];
[theItems addObject:item];
[theItems addObject:item2];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
[self.navigationController setToolbarHidden:NO animated:YES];
[self.navigationController setToolbarItems:theItems animated:NO];
//self.navigationController.toolbarItems = theItems; // Tried both

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
label.text = @"kjhdkjhadsda";
[self.navigationController.toolbar addSubview:label];

This only shows the UILabel in the correct position, nothing else appears. The UILabel is useless for me, it is just a test. I also tried to allocate a new array instead of copying the one from the component. Ignore the missing releases, this is only test code.

I read many questions about this but no answer seem to help making it work. Any idea what might not be ok in this code?

Luca Carlon
  • 9,546
  • 13
  • 59
  • 91

1 Answers1

2

Seems that you are trying make mutable copy of nil in line

[self.navigationController.toolbar.items mutableCopy];

By default method navigationController.toolbar.items nas no items and returns nil

Update

Method - (void)setToolbarItems:(NSArray*)toolbarItems animated:(BOOL)animated does nothing if you send it to UINavigationController. You need to set toolbar items to that controller who is managed by a navigation controller. This line will make your buttons visible:

[self setToolbarItems:theItems animated:NO];
Mark Kryzhanouski
  • 7,251
  • 2
  • 22
  • 22
  • I specified I also tried to allocate a new one. Maybe it was not clear I was referring to that one, sorry. – Luca Carlon Mar 06 '13 at 12:35
  • Did you check if your self.navigationController != nil. Your code looks ok and should work. And also try to set toolbar items via UIViewController's method [self setToolbarItems:theItems animated:NO]; – Mark Kryzhanouski Mar 06 '13 at 12:41
  • Il self.navigationController was nil I wouldn't expect the toolbar to be shown and the label to be added correctly, right? I just tried the method setToolbarItems from the UIViewController, didn't know it existed either, but no button appears. Any other idea? Maybe the code is ok but there might be something else that prevents button to show? – Luca Carlon Mar 06 '13 at 12:56
  • Seems you do not understand me correctly. I've tried to say that self.navigationController might be nil at this method and then be accessible later. Check this, please. BTW, in which moment have you tried to access navigationController(in loadView,viewDidLoad or viewDidAppear)? – Mark Kryzhanouski Mar 06 '13 at 13:02
  • viewDidLoad and this is the only method where I manipulate the toolbar. So I show it, and it is shown, I add the buttons, and those are not shown, and I place the label, which is shown. Checked for nil self.navigationController and it is not nil. – Luca Carlon Mar 06 '13 at 13:13
  • I've got it work through [self setToolbarItems:theItems animated:NO]; – Mark Kryzhanouski Mar 06 '13 at 13:17
  • I can confirm that both using the method from the UIViewController and avoid using the existing array (nil as you said) is working. All the other methods are not. Thank you! – Luca Carlon Mar 06 '13 at 13:37