The status of the UIViewController button is always the same

I added the "Edit" button to the navigation bar in my application.

I have a UIViewController that implements UITableViewDataSource and UITableViewDelegate.

I added a UITableView using the storyboard and made connections to the data source and delegation.

I liked this in the view controller implementation file:

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 click the β€œEdit” button, the β€œEdit” variable of BOOL is always β€œYES”. And for the first, it sets the UITableView mode to edit mode, but the Edit button still shows the Edit label instead of Finish. And since the parameter is always YES, the table view is never set to normal.

I read from the answer here: UITableView in edit mode - the "Edit" button does not change the status

I assume that the "Change" button should change its state when clicked. And the parameter in the overridden method should also switch.

I can write my own code to set the flags in the UIViewController to check the mode and switch the View table accordingly, but I assume there must be some other way.

+4
source share
2 answers

From the documentation

enter image description here

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

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

Thus, you just have to override this method and invoke super-execution from above, as indicated in the documentation.

+6
source

I think that 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 (in this case, your right button), your target may have one of the following signatures.

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

You must handle the editing mode as part of this method separately.

0
source

Source: https://habr.com/ru/post/1437225/


All Articles