0

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

Dr_Mom
  • 63
  • 8

3 Answers3

0

to change the navigationItem button into Done button use this,

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];

to change the navigationItem button into Edit button use this,

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered];

If you have added the button through IB Storyboard, then make sure to set the identifier. Then add in your code do the following:

editButton.title = @"Done"; 
//or
editButton.title = @"Edit";
Antony Raphel
  • 2,036
  • 2
  • 25
  • 45
  • 1
    Thanks, but it's not what I'm looking for. As I said below I don't want to set the title manually because of localization, for example. – Dr_Mom Apr 03 '17 at 10:34
0

Try This

- (void)viewDidLoad
{
[super viewDidLoad];
 UIBarButtonItem *leftLoginBar=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(myProfileClickOnDashboard)];
leftLoginBar.tintColor=White;
self.tabBarController.navigationItem.leftBarButtonItem = leftLoginBar;
}



-(void)myProfileClickOnDashboard
{
UIBarButtonItem *rightLoginBar=[[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(filterClickOnDashboard)];
rightLoginBar.tintColor=White;
self.tabBarController.navigationItem.leftBarButtonItem = rightLoginBar;

//do ... when your bar button is Edit

}
-(void)filterClickOnDashboard
{
UIBarButtonItem *leftLoginBar=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(myProfileClickOnDashboard)];
leftLoginBar.tintColor=White;
self.tabBarController.navigationItem.leftBarButtonItem = leftLoginBar;

    //do... when bar button is Done.
}
Dishant Rajput
  • 1,329
  • 1
  • 10
  • 20
0

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.

Dr_Mom
  • 63
  • 8