Search code examples
iphonexcode4interface-builderuibutton

Why would this not work? if ( [mybutton isSelected] )


I have 32 buttons in my GUI... (don't ask)...

Anyway, if only select (by tapping) four of the buttons then I'd like to something for those buttons that are selected.

My problem is that when I check each button's "selected" property, they all return true.

I am also using the interface builder and it is there that I have specified an icon to display on the button whenever it is in the selected state. Each button also has a tag value ranging from 100 to 132. The icon for selected buttons is working great! When you tap 4 buttons then those 4 buttons have the icon and no others... However, when I run the code to detect which are selected, all of them return true.

Any ideas what I am doing wrong?

for (int j = 1; j <= 32; j++) {
        but = (UIButton *)[self.view viewWithTag:(j + 100)];
        if(but.selected == YES) {
            // Note: I have also tried if ([but isSelected])
            //       I have also tried if ([but isSelected] == YES)
            //
            // every button is entering this section of code...
            //
        }
}

Solution

  • Check the sender of button_clicked Method instead of checking "but.selected" . It should be only one from 32 buttons when you click a button.

    replace

    but.selected == YES
    

    with

    but == sender
    

    I think this will solve your problem definitely.