문제

I have OutletCollection of buttons

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

I need to change the backgroundImage of buttons which are pressed.

I try to make this for each

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

But when i press on a button with the tag "1", image changes on the button with 1 and 2 and so on. Thanks for answers...

도움이 되었습니까?

해결책

You are looping through all buttons and changing the image on each with a tag matching your if statements, you need to check that the button is the selected one, e.g.

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

or if you want to use tags

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

다른 팁

Resolved...

for (UIButton *btn in _someBtn) {
NSLog(@"pressed button is:%ld", (long)btn.tag)
if ([btn isTouchInside]) {
[btn setBackgroundImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];
}
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top