单击该按钮时,用另一个图像覆盖图像按钮

时间:2012-09-28 06:33:32

标签: iphone objective-c

我在 cellForRow 方法的每一行前面创建了一个图像按钮( a.png )。在同一方法本身我调用 buttonPressed 方法,该方法具有按下按钮时要执行的代码。我希望按下按钮时,按钮图像应该更改为相同尺寸的另一个图像( b.png )。

请帮助解决这个问题。

4 个答案:

答案 0 :(得分:0)

[but setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
[but setBackgroundImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateSelected];

试试这个......

答案 1 :(得分:0)

在自定义单元格上为UITableView和UIButton创建自定义单元格。

为UIButtion创建属性。

在cellForRowAtIndexPath:方法

//Set Action on  Button  in Table View Row

[cell.UIButtonOutlet addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside]; 

这将从您的按钮创建操作。

方法

-(void)checkButtonTapped:(id)sender event:(id)event
{

NSSet *touches=[event allTouches];

  UITouch *touch=[touches anyObject];

 CGPoint currentTouchPosition = [touch locationInView:self.historyTableView];

//GET IndexPath

NSIndexPath *indexPath=[self.TableView indexPathForRowAtPoint: currentTouchPosition];        

 selectedCell= [PairTableView cellForRowAtIndexPath:indexPath];

[selectedCell.UIButtonOutlet setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];

答案 2 :(得分:0)

尝试以下方法

 UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
        [button setFrame:CGRectMake(12,12,200,200)];
        [button addTarget:self action:@selector(click: withEvent:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:indexPath.row];
        [button setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];

        [cell.contentView addSubview:button];

然后在按钮的点击操作方法

    -(void)click:(UIButton *)button :withEvent:(UIEvent *)events{
       [self yourFunction:(UIButton *)button];

    }

然后终于

   -(void)yourFunction:(UIButton *)button{
     button.selected = ! button.selected;

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

                }

}

答案 3 :(得分:-1)

初始化按钮时尝试此操作

UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];

然后修改你的方法(在按下按钮时调用),就像这样

-(void)yourMethod:(id)sender{

    UIButton *btn=(UIButton*)sender;
    [btn setImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateNormal];

    //your other code

}
相关问题