更改背景出口集合中按钮的图像

时间:2014-05-13 11:01:49

标签: ios uibutton

我有OutletCollection按钮

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *someBtn;

我需要更改按下的按钮的backgroundImage。

我试着为每个

做这个
for (UIButton *btn in _someBtn) {
if (btn.tag == 1) {
[btn setBackgroundImage:[UIImage ....................:forControl....]
} else if (btn.tag == 2) {
[btn setBackgroundImage:UIImage ......................]
}

但是当我按下带有标签" 1"的按钮时,按钮上的图像会改变为1和2,依此类推。 谢谢你的回答...

2 个答案:

答案 0 :(得分:0)

您循环浏览所有按钮并使用与if语句匹配的标记更改每个按钮上的图像,您需要检查按钮是否为选定按钮,例如

for (UIButton *button in _someBtn)
{
    if (button == selectedButton)
    {
        [button setBackgroundImage:someImage forState:UIControlStateNormal];
    }
}

或者如果你想使用标签

UIButton *button = [buttonSuperview viewWithTag:whicheverTag];
[button setBackgroundImage:someImage forState:UIControlStateNormal];

答案 1 :(得分:0)

...解决

for (UIButton *btn in _someBtn) {
NSLog(@"pressed button is:%ld", (long)btn.tag)
if ([btn isTouchInside]) {
[btn setBackgroundImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];
}
}