Cambiar un estado de UIBarButtonItem mediante programación

I have an Edit/Done UIBarButtonItem, and I also have a Cancel button in the same view.

If the user hits the cancel button while in editing mode, I want to set the Edit button back to its original state.

But I don't see any way the change the state of a UIBarButtonItem programmatically. Can this be done? Or is there another way to get the same effect?

preguntado el 09 de enero de 11 a las 00:01

2 Respuestas

With more research, it turns out the right way to do this is to change the editing state on the view controller. I was trying to change the editing state of the table view, and the Edit/Done button independently. Instead, when a user hits the Cancel button while in editing mode, simply do this while handling the Cancel button:

if (self.editing) {
    self.editing = NO;

This will change the state of the tableView, and the Edit/Done button, so the next time the view is displayed, the view is reset back to it's normal (non-editing) state.

Respondido el 09 de enero de 11 a las 22:01

How about just change the title and having a BOOL yourself to store the current state of the button? Like this:

- (IBAction)cancelPressed {
   self.isBarButtonEditing = !self.isBarButtonEditing;
   if (self.isBarButtonEditing) {
      self.title = @"Done";

Respondido el 09 de enero de 11 a las 04:01

That's a good idea, but won't work for two reasons. The button changes to blue after it is initially tapped, and I'd like to get it back to it's original color. Second, if I do this, another tap on the button results in a color change, but no title change. So I really need to change the true state of the button. - Don Wilson

oh, you mean the general edit button in a UITableView, right? - vodkhang

maybe you should try with [self.tableView setEditing:NO animated:NO] - vodkhang

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.