我有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,依此类推。 谢谢你的回答...
答案 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];
}
}