IOS: UIBarButtonItem创建后如何重置Action?
IOS: How to Reset Action of UIBarButtonItem after it has been created?
我有一些代码可以创建 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);
}
最好使用 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];
我有一些代码可以创建 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);
}
最好使用 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];