1

I am trying to alter the visible buttons on the toolbar depending on the state of the table view. Is this possible?

To add more detail: I currently have a list of exercises in a table view. There is a toolbar at the bottom with a button on it "new" allowing the user to create a new exercise. When the user clicks "edit" in the top right the view goes into editing mode and I want them to be able to delete exercises using multiple selection by clicking a now unhidden "delete" button in the toolbar. At this point I am trying to make the "new" button disappear and leave only the "delete" button.

I can find lots of answers of how to add toolbars and add buttons to them but none seem to be dealing with this once the view has loaded.

I have thought about having two different toolbars with the separate buttons on each and then switching which is hidden and which is visible but this will lead to other problems so I am exploring whether there is an easy way to only hide the added buttons.

sam_smith
  • 6,023
  • 3
  • 43
  • 60
  • Just update the view controller's `toolbarItems` property with the new buttons. This assumes the view controller is in a navigation controller. – rmaddy Oct 31 '13 at 14:55

1 Answers1

0

Its really easy. Lets First look how to create a UIToolbar then addd items to it and then change them during run time

self.MNToolbar=[[UIToolbar alloc] init];

self.addButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(importPhotos:)];

self.flexibleSpace=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

self.MNToolbar.items=[[NSArray alloc] initWithObjects:flexibleSpace, addButton, flexibleSpace, nil];

[self.view addSubView:self.MNToolbar];
// don't forget to set the bounds too

This is how to create it.

Ok now lets see how to alter it during runtime

self.deleteButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(deletePhotos:)];

self.MNToolbar.items=[[NSArray alloc] initWithObjects:self.deleteButton, nil];

I hope you get the point. If you have any questions let me know.

Muhammad Nasir
  • 1,796
  • 1
  • 14
  • 22
  • Why are you creating a `UIToolbar`? Assuming the view controller is in a navigation controller, the navigation already provides the toolbar. Just set the view controller's `toolbarItems` property to the new toolbar items array. – rmaddy Oct 31 '13 at 14:55
  • As he stated "There is a toolbar at the bottom with a button on it". so he is not talking about the navigation controller. – Muhammad Nasir Nov 01 '13 at 10:39