Abs3akt Abs3akt - 1 month ago 8
Objective-C Question

How to set title Edit and Done for Button from UIBarButtonItem?

I have a problem,Top button does not change own name but it works

- (IBAction)switchEdit:(UIBarButtonItem *)sender {
sender.possibleTitles = [NSSet setWithObjects:@"Edit", @"Done", nil];
if (sender.style == UIBarButtonSystemItemEdit){
sender.style = UIBarButtonSystemItemDone;
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
self.tableView.editing = NO;
}else{
sender.style = UIBarButtonSystemItemEdit;
[self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
self.tableView.editing = YES;
}
}

phi phi
Answer

Consider using UIViewController's editButtonItem property, since it seems to do automatically what you need:

Returns a bar button item that toggles its title and associated state between Edit and Done.

If you want to keep your approach, you can declare these two buttons as properties and set them up in viewDidLoad for example, like that:

@interface ViewController ()

@property (nonatomic, strong) UIBarButtonItem *editBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *doneBarButtonItem;

@end

and then

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.editBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editSelectorName)];
    self.doneBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneSelectorName)];
}

and then in your code just do:

self.navigationItem.rightBarButtonItem = self.editBarButtonItem

or

self.navigationItem.rightBarButtonItem = self.doneBarButtonItem