2

I have a UIViewController which has a UITableView as a child besides other elements. I want to add editing support on that tableView with this simple line:

self.navigationItem.rightBarButtonItem = self.editButtonItem;

It works if I set this on UITableViewController view type, but obviously that does not work on tableView's that are subviews. Actually it shows the editing button but the edit action is not triggered.

Anyway if there is no elegant solution for that I will simply implement custom editing button.

Thanks!

Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91

4 Answers4

3
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"My View Controller";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButtonSelected:)] autorelease];

}

- (void) editButtonSelected: (id) sender
{
    if (tableView1.editing) {
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButtonSelected:)] autorelease];
        [self.tableView1 setEditing:NO animated:YES];
    } else {
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editButtonSelected:)] autorelease];
        [self.tableView1 setEditing:YES animated:YES];

    }

}
Apple.SL
  • 81
  • 3
2

You need to implement setEditing:animated yourself:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [self.tableView setEditing:editing animated:animated];
    [super setEditing:editing animated:animated];
}
utopalex
  • 1,324
  • 10
  • 9
1

I've just found a solution. Replacing self.editButtonItem with self.myTableViewController.editButtonItem like that:

self.navigationItem.rightBarButtonItem = self.myTableViewController.editButtonItem;

It works like a charm. Could it be more simple? :)

Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91
0

self.navigationItem.leftBarButtonItem = self.editButtonItem;

hayden
  • 293
  • 1
  • 5