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];
}
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.
}
Change button style or use builtin self.editButtonItem;
check this - How to change UIBarButtonItem's type in UINaviagationBar at runtime?
Cheers, Krzysztof Zabłocki
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With