0

一旦单击该按钮,我想在表格视图控制器中的另一个图像按钮顶部添加图像。我在 .h 文件中有 UIButton *button。

    CellForRow:{
    button=[[UIButton alloc]initWithFrame:CGRectMake(230,0,40,40); 
   [button addTarget:self action:@selector(buttonPressed:withEvent:)                 forControlEvents:UIControlEventTouchUpInside]; 
[button setImage:[UIImage imageNamed:"a"] forState:UIControlStateNormal];
button.tag=indexPath.row;
[cell addSuBView:button];
[cell setIndentationWidth:1];
[cell setIndendationLevel:1];
}
-(void)buttonPressed:(id) sender withEvent: (UIEvent *) event{
UIButton *mybtn=(UIButton*) sender;
[mybtn setImage:[UIImage imageNamed:"b"] forState:UIControlStateNormal];
}

这样做是在第一个图像之上显示第二个图像。但我想要的是用新形象代替旧形象。

4

1 回答 1

0
-(void)buttonPressed:(id)sender withEvent:(UIEvent *)event{
UIButton *button = (UIButton *)sender;
button.selected = ! button.selected;

                if (button.selected) {
                    [button setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
                }
                else{
                    [button setBackgroundImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateNormal];

                }

}
于 2012-09-28T14:03:38.840 回答