我在这里使用默认的 Bar Button Item editButtonItem:
@property (nonatomic, assign) BOOL isEditing;
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self setupInterface];
}
- (void)setupInterface {
if (self.dataElement) {
self.isEditing = NO;
self.nameTextField.text = self.dataElement.name;;
} else {
self.isEditing = YES;
self.deleteButton.hidden = YES;
}
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
editing = self.isEditing;
[super setEditing:editing animated:YES];
if (editing) {
[self saveAction];
[self.navigationController popViewControllerAnimated:YES];
} else {
self.isEditing = !self.isEditing;
}
}
- (void)setIsEditing:(BOOL)isEditing {
_isEditing = isEditing;
if (isEditing) {
self.deleteButton.hidden = NO;
self.nameTextField.userInteractionEnabled = YES;
} else {
self.deleteButton.hidden = YES;
self.nameTextField.userInteractionEnabled = NO;
}
}
但是,它不会将按钮的标题从 Edit 更改为 Done。我知道,它可以自动更改,不想像这样分配它: self.editButtonItem.title = @"Done";