iosobjective-cuinavigationbaruinavigationitemsetediting

editButtonItem doesn't change Edit/Done title


I use a default Bar Button Item editButtonItem here:

@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;
    }
}

However it doesn't change title of a button from Edit to Done. I know, that it can be changed automatically and don't want to assign it like: self.editButtonItem.title = @"Done";


Solution

  • Found a solution: We have to use only - (void)setEditing:(BOOL)editing animated:(BOOL)animated method.

    It could be useful for someone:

    - (void)viewDidLoad {
      [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self setupInterface];
    }
    
    - (void)setupInterface {
    
        if (self.dataElement) {
            self.nameTextField.text = self.dataElement.name;
        }
    }
    
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
      [super setEditing:editing animated:YES];
    
        if (editing) {
            self.deleteButton.hidden = (self.dataElement == nil);
            self.addImageButton.hidden = NO;
            self.nameTextField.userInteractionEnabled = YES;
    
        } else {
            self.deleteButton.hidden = YES;
            self.addImageButton.hidden = YES;
            self.nameTextField.userInteractionEnabled = NO;
    
            [self saveAction];
        }
    }
    

    For now everything works right.