0

我在这里使用默认的 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";

4

3 回答 3

0

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

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];

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

[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered];

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

editButton.title = @"Done"; 
//or
editButton.title = @"Edit";
于 2017-04-03T09:59:35.907 回答
0

尝试这个

- (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.
}
于 2017-04-03T16:29:31.547 回答
0

找到了解决方案:我们只能使用 - (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];
    }
}

现在一切正常。

于 2017-04-05T11:05:38.903 回答