我有一些代码可以创建barbuttonitem Edit,如果单击它并开始编辑,请将按钮更改为Done。
当我第一次创建按钮时,我将其动作设置为编辑。但是,一旦用户编辑,我想将重命名的按钮的操作更改为保存。
我以为我已经保存了射击,但是当我在中间添加一个方法来启用和禁用按钮时,它可能会停止工作。
有人可以建议更改条形按钮项的操作的正确方法吗?
这是我的代码:
//code to create button which sets action to gotoEdit method
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(gotoEdit)];
self.navigationItem.rightBarButtonItem = editButton;
}
-(void) gotoEdit {
self.navigationItem.rightBarButtonItem.title = @"Done";
_editButton.target = self;
_editButton.action = @selector(save);//changes action to save method
//some other code to make a textview editable, change its background color and so forth.
}
-(void) save {
NSLog(@"save method firing");
}
//I added the following methods at about the same time the save method stopped firing but not sure if they are related. (Probably not but including them anyway.)
//detect change on screen
- (void)textViewDidChange:(UITextView *)textView{
self.didChange=YES;
[self updateSaveButton];
}
-(void) updateSaveButton
{
self.editButton.enabled = (_didChange == TRUE);
}
答案 0 :(得分:1)
最好使用bool
,而不是添加和删除按钮的操作。
-(void) gotoEdit {
if(!isEditing){
// Prep for editing
self.navigationItem.rightBarButtonItem.title = @"Done";
isEditing = true;
}else{
// Prep for saving
self.navigationItem.rightBarButtonItem.title = @"Edit";
isEditing = false;
}
}
为了将来参考,您可以删除按钮的选择器,如下所示:
[_editButton removeTarget:self
action:@selector(gotoEdit)
forControlEvents:UIControlEventTouchUpInside];