Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone, how do I toggle my Edit button for my table then to Done and back to Edit?

I've added my edit button

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
        initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self 
        action:@selector(editNavButtonPressed)] autorelease];

But I don't seem to be able to turn it to Done and back, the console says its Null

-(IBAction)editNavButtonPressed
{
//[self.tableView setEditing:YES animated:YES];
BOOL editing = !self.tableView.editing;
NSLog(@"tile=#%@#", self.navigationItem.rightBarButtonItem.title);

if ([self.navigationItem.rightBarButtonItem.title isEqualToString:NSLocalizedString(@"Edit", @"Edit")]) {
    self.navigationItem.rightBarButtonItem.title = NSLocalizedString(@"Done", @"Done");
} else {
    self.navigationItem.rightBarButtonItem.title = NSLocalizedString(@"Edit", @"Edit");

}
//self.navigationItem.rightBarButtonItem.enabled = !editing;
//self.navigationItem.rightBarButtonItem.title = (editing) ? NSLocalizedString(@"Done", @"Done") : NSLocalizedString(@"Edit", @"Edit");
[self.tableView setEditing: editing animated: YES];

}

like image 371
Jules Avatar asked Oct 06 '10 11:10

Jules


2 Answers

You should use the -[UIViewController editButtonItem], it will correctly toggle state from Edit/Done, and will also toggle the editing mode on the UIViewController itself.

Setup with something like this:

-(void)viewDidLoad {
   self.navigationItem.rightBarButtonItem = [self editButtonItem];
}

You can then override -[UIViewController setEditing:animated:] to get notified immediately when the editing mode is toggled.

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    // Add your custom code here
}

Or you can query the UIViewController for it's current editing state like so:

if ([self isEditing]) {
    // Do something editing like
} else {
    // Do whatever is not editing like.
}
like image 142
PeyloW Avatar answered Oct 20 '22 00:10

PeyloW


Change button style or use builtin self.editButtonItem;

check this - How to change UIBarButtonItem's type in UINaviagationBar at runtime?

Cheers, Krzysztof Zabłocki

like image 24
Krzysztof Zabłocki Avatar answered Oct 20 '22 01:10

Krzysztof Zabłocki