In my table view controller there is
self.navigationItem.leftBarButtonItem = self.editButtonItem;
which produces a regular edit/done button at
For those who don't want to override UITableView (e.g. if you're using UITableViewController), here's a simple and clean solution that I use. It basically involves creating your own edit button item and using the tableView's editing
flag to track edit vs done. For icing on the cake, it shows a "+" button (instead of "Edit") when the table is empty for adding new items.
- (void) updateEditButtonVisibility
{
// tableItems represents the data structure that s
if ([tableItems count] > 0)
{
UIBarButtonSystemItem editButtonType = self.tableView.editing ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit;
UIBarButtonItem *editButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:editButtonType
target:self
action:@selector(editButtonTouched)];
self.navigationItem.rightBarButtonItem = editButtonItem;
[editButtonItem release];
}
else
{
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addButtonTouched)];
self.navigationItem.rightBarButtonItem = addButtonItem;
[addButtonItem release];
}
}
- (void) editButtonTouched
{
// edit/done button has been touched
[self.tableView setEditing:!self.tableView.editing animated:YES];
[self updateEditButtonVisibility];
}
- (void) addButtonTouched
{
// logic to allow user to add new items goes here
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self updateEditButtonVisibility];
}
for those who are still interesed in this question (or answer :P)
UITableView API
revealed that there is a - (void)setEditing:(BOOL)editing animated:(BOOL)animate
method
these method is called every time this edit/done button is pressed. you have to simply check by the (BOOL)editing
parameter wich one was used. last but not least you have to call the proper method from the originally edit/done button.
simply add this method to your uitableview class
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if(editing)
{
NSLog(@"editMode on");
}
else
{
NSLog(@"Done leave editmode");
}
}
It is possible to change the action. After edit button clicked it shows delete button, instead it is possible to show reject/verify/modify buttons. And change corresponding action instead of delete option
Thanks Mindus
This is the standard way to get notified when a bar button has been pushed:
self.editButtonItem.target = self;
self.editButtonItem.action = @selector(buttonPushed:);
...
- (void) buttonPushed:(id)sender
{
// do stuff here
}