1

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.

Braiam
  • 1
  • 11
  • 47
  • 78
zinkocoder
  • 19
  • 1
  • 3
  • In your last approach your method signature is different. In initialization, it has "toggleDelete:" while implemented method is not taking any parameters. Try changing this and see if works. – Amit Vaghela May 04 '11 at 00:43
  • Thanks, Amit. I may be missing what you're suggesting but I changed it to @selector(toggleDelete) (i.e., no colon) and got the same result. Note that toggleDelete *is* getting called whenever either button is clicked; I've confirmed that breakpoints get hit each time. Is that what you were suggesting I try? – zinkocoder May 04 '11 at 04:36
  • Well, just learned from this [question](http://stackoverflow.com/questions/4953623/when-to-use-a-colon-with-a-selector) how the colons work with selectors. I'll edit the question to use the version without colons as that seems to be the correct usage (not sure why it actually the method is getting called when the colons are present but I'll look into that separately. – zinkocoder May 04 '11 at 04:44

1 Answers1

0

You might already be doing this but since I can't see it in the code, I'll mention it anyway. Once you set the deleteButton to either doneButton or trashButton, you should set the deleteButton to the proper barItem as well. After the else statement, maybe something like this:

self.navigationItem.rightBarButtonItem = self.deleteButton;
Bittu
  • 676
  • 2
  • 8
  • 22