Domanda

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...

È stato utile?

Soluzione

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];

Altri suggerimenti

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];
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top