Changing a UIBarButtonItem state programmatically
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?
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.
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";
}
}
精彩评论