如何在单击按钮操作时更改图像

时间:2014-07-23 12:15:04

标签: objective-c uitableview uiimageview uibutton

我有编辑按钮来编辑和删除按钮以删除操作。如果我单击编辑按钮,我需要显示完成按钮图像。然后用户再次完成删除操作按完成我需要显示带图像的编辑按钮。以前我用文字做过。如何处理图像?

 UIImage *bg = [UIImage imageNamed:@"edit.png"];
 [editButton setBackgroundImage:bg forState:UIControlStateNormal];
 //  [editButton setTitle:@"Edit" forState:UIControlStateNormal];

 -(void)editButtonPressed:(UIButton *)button{



 NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

 [tableview setEditing:![tableview isEditing] animated:YES];

 NSString *buttonTitle = ([tableview isEditing]) ? @"Done" : @"Edit";

 [editButton setTitle:buttonTitle forState:UIControlStateNormal];

 NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);


 }

3 个答案:

答案 0 :(得分:0)

您可以使用图片执行完全相同的操作:

UIImage *bg = [UIImage imageNamed:@"Edit"];
 [editButton setImage:bg forState:UIControlStateNormal];
 //  [editButton setTitle:@"Edit" forState:UIControlStateNormal];

 -(void)editButtonPressed:(UIButton *)button{



 NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

 [tableview setEditing:![tableview isEditing] animated:YES];

 NSString *buttonImageName = ([tableview isEditing]) ? @"Done" : @"Edit";

 [editButton setImage:[UIImage imageNamed: buttonImageName] forState:UIControlStateNormal];

 NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);


 }

答案 1 :(得分:0)

UIImage *img=[(UIButton *) sender currentImage];   

if(img == [UIImage imageNamed:@"edit"])
{  
    //If do something
 }

试试这样。

答案 2 :(得分:0)

您可以将图像设置为正常状态和选定状态。按下切换按钮状态进入选中状态(表格将处于编辑模式),再次按下切换按钮状态恢复正常。

- (void)viewDidLoad
{
    // ...
    [editButton setImage:normalImage forState:UIControlStateNormal];
    [editButton setImage:editmodeImage forState:UIControlStateSelected];
}

- (void)editButtonPressed:(UIButton *)button
{
    tableView.editing = !tableView.editing;
    editButton.selected = tableView.isEditing;
    // ...
}