0

我的VC 中有一个UIButton这样的单元格UITableView

arrowBtnUp = [UIButton buttonWithType:UIButtonTypeCustom];
arrowBtnUp.frame = CGRectMake(50,(65-19)/2,31, 31);
[arrowBtnUp setBackgroundImage:[UIImage imageNamed:@"arrow_up.png"] forState:UIControlStateNormal];
[arrowBtnUp addTarget:self action:@selector(slideAction) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:arrowBtnUp];

这是我的slideAction

-(void) slideAction
{
    [arrowBtnUp setBackgroundImage:[UIImage imageNamed:@"arrow_down.png"] forState:UIControlStateNormal];
    // also tried  with UIControlStateSelected/Highlighted
}

但它没有用。我找到了这个链接但没有帮助。任何建议或样品将不胜感激。

4

5 回答 5

4

您的代码应该可以工作。无论如何更改您的代码并尝试

-(void) slideAction:(id)sender
{
   [sender setBackgroundImage:[UIImage imageNamed:@"arrow_down.png"] forState:UIControlStateNormal];
}

[arrowBtnUp addTarget:self action:@selector(slideAction:) forControlEvents:UIControlEventTouchUpInside];

并确保您的 app bundle 中有 arrow_down.png

于 2012-11-21T11:52:56.173 回答
3

问题是您正在重用arrowBtnUpUIButton 的实例。因此,在该slideAction方法中,您不会获得按下的按钮参考。

要在方法中获取引用,slideAction您需要将其作为参数传递。

因此,将代码更改为:

[arrowBtnUp addTarget:self action:@selector(slideAction:) forControlEvents:UIControlEventTouchUpInside];


-(void) slideAction:(UIButton *)myButton
{
   [myButton setBackgroundImage:[UIImage imageNamed:@"arrow_down.png"] forState:UIControlStateNormal];
}
于 2012-11-21T11:58:08.657 回答
1

If you're trying to change the button image after a touch, you should use the UIButton's control states. You can assign a background image for each "state" and let UIButton determine which to show.

于 2013-06-14T10:29:11.297 回答
0

背景图像与按钮上的图标不同。试试这个:

[_buttonConfirm setImage:[UIImage imageNamed:@"s.png"] forState:UIControlStateNormal];
于 2012-11-21T11:59:05.707 回答
0

If you're trying to change the button image after a touch, you should use the UIButton's control states. You can assign a background image for each "state" and let UIButton determine which to show.

[myButton setBackgroundImage:[UIImage imageNamed:@"arrow_up.png"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:@"arrow_down.png"] forState:UIControlStateSelected];

There is no need to manually switch the background images in your slideAction: target method.

于 2012-11-21T12:09:13.490 回答