editButtonItem 不会更改 Edit/Done 标题

editButtonItem doesn't change Edit/Done title

我在这里使用默认的条形按钮项 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;
    }
}

但是它不会将按钮的标题从“编辑”更改为“完成”。 我知道,它可以自动更改并且不想像这样分配它: self.editButtonItem.title = @"Done";

navigationItem 按钮更改为 完成 按钮使用此按钮,

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];

要将 navigationItem 按钮更改为 编辑 按钮,请使用此按钮,

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered];

如果您是通过 IB Storyboard 添加按钮,请务必设置标识符。然后在您的代码中添加执行以下操作:

editButton.title = @"Done"; 
//or
editButton.title = @"Edit";

试试这个

- (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.
}

找到解决方法:我们只需要使用 - (void)setEditing:(BOOL)editing animated:(BOOL)animated 方法。

它可能对某人有用:

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

目前一切正常。