I've got a UIToolbar with a button that I'd like to use for toggling Edit mode to enable deleting rows in my UITableView. I've got everything working perfectly if I settle for just changing the title of the UIBarButton dynamically. I.e., You start the app and the button says "Delete". Click on it and the UITableView switches into 'Edit' mode and I change the button's title to 'Done'. Click it again and it leaves 'Edit' mode and the button goes back to the "Delete" title.
What I'm failing to get to work is having the button show the standard trash icon instead of "Delete." I noticed the UIBarButtonSystemItemTrash constant and it certainly shows the trash icon if I start by using it but I can't get it to switch to the 'Done' button if I do.
Here's the code that works for the Title changing approach:
- (IBAction)toggleDelete
{
[self.eventsTable setEditing:!self.eventsTable.editing animated:YES];
if (self.eventsTable.editing)
{
[self.deleteButton setTitle:@"Done"];
[self.deleteButton setStyle:UIBarButtonItemStyleDone];
}
else
{
[self.deleteButton setTitle:@"Delete"];
[self.deleteButton setStyle:UIBarButtonItemStyleBordered];
}
}
I've tried several other approaches but none have worked. Here's one that pretty much seems to be ignored by the system:
- (IBAction)toggleDelete
{
[self.eventsTable setEditing:!self.eventsTable.editing animated:YES];
if (self.eventsTable.editing)
[self.deleteButton setStyle:UIBarButtonSystemItemDone];
else
[self.deleteButton setStyle:UIBarButtonSystemItemTrash];
}
and here's another hack at it that certainly seemed hacky but I found someone claiming it would work. It unfortunately didn't for me though but it felt extra hacky anyway. It too seems to be ignored by the system; what I mean by that is that whatever I set the button to start off as in Interface Builder is what it is even though this code is executing.
- (IBAction)toggleDelete
{
[self.eventsTable setEditing:!self.eventsTable.editing animated:YES];
if (self.eventsTable.editing)
{
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(toggleDelete)];
self.deleteButton = doneButton;
[doneButton release];
}
else
{
UIBarButtonItem *trashButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self action:@selector(toggleDelete)];
self.deleteButton = trashButton;
[trashButton release];
}
}
I saw a couple other questions close to my scenario but none ended up helping.