4

I added an Edit button to Navigation bar in my app.

I have a UIViewController which implements UITableViewDataSource and UITableViewDelegate.

I added the UITableView using storyboards and made connections to data source and delegate.

I did like this in implementation file of the view controller:

self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self.editButtonItem setAction:@selector(setEditing:animated:)];

and

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    DLog(@"Editing = %d", editing)
    NSLog(editing ? @"Yes" : @"No");

    [self.recentSearchList setEditing:editing animated:YES];
}

The problem is that whenever I tap on Edit button, the BOOL variable "editing" is always "YES". And for the first it sets the UITableView to editing mode but the Edit button still shows "Edit" label instead of "Done". And the since the parameter is always YES, the table view is never set to normal mode.

I read from an Answer in here: UITableView in edit mode - 'Edit' button doesn't change status

I am assuming the Edit button should change its state itself on tapping. And the parameter in the overridden method should also toggle.

I can write code of my own to set flags in the UIViewController to check the mode and toggle the tableView accordingly but I am assuming that there must be some other way.

4

2 回答 2

6

From the documentation

enter image description here

Also remove the line [self.editButtonItem setAction:@selector(setEditing:animated:)];

The editButtonItem gets implicitly associated with the method - (void)setEditing:(BOOL)editing animated:(BOOL)animated.

So you just gotta override that method and call super's implementation at the top as the documentation says.

于 2012-10-02T10:10:34.550 回答
0

I think if you look more closely you will see that editing is actually a reference to the control itself.

When you call setAction on a UIControl ( your right button in this case ) your target can have one of the following signatures.

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)

You have to handle edit mode within this method separately.

于 2012-10-01T16:13:49.187 回答