views:

58

answers:

2

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];

}

+1  A: 

Change button style or use builtin self.editButtonItem;

check this - http://stackoverflow.com/questions/1648244/how-to-change-uibarbuttonitems-type-in-uinaviagationbar-at-runtime

Cheers, Krzysztof Zabłocki

Krzysztof Zabłocki
Yes but I also need to determine what the button is set too now. I did find this http://stackoverflow.com/questions/2020716/rename-standard-edit-button-in-tableview but the setEditing function is never called.
Jules
+1  A: 

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 editBarButtonItem];
}

You can then override -[UIViewController setEditing:animated:] to get notified emidiately 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.
}
PeyloW