Duck
Duck

Reputation: 35953

iPhone . button image not switching

I have implemented a radio button on one of my apps, exactly like this solution:

http://www.developers-life.com/radio-buttons-in-iphone-application.html

but my checkboxButton method is like this:

- (IBAction)checkboxButton:(UIButton *)button{

     for (UIButton *but in self.arrayButtons) {
         if (but == button)
             [but setSelected:YES];
         else
             [but setSelected:NO];
     }
}

I have two buttons belonging to the same radio button group. When I select one of the buttons its image toggles to the switched one. Perfect. The problem is that when I select the other one it will not select to selected state and the first one that is being displayed as selected continues to be displayed as selected.

any clues?

Upvotes: 0

Views: 151

Answers (5)

Duck
Duck

Reputation: 35953

After trying all solutions given here and have all failed, I tried a new approached and it worked perfectly.

The solution was to change this

- (IBAction)checkboxButton:(UIButton *)button{

     for (UIButton *but in self.arrayButtons) {
         if (but == button)
             [but setSelected:YES];
         else
             [but setSelected:NO];
     }
}

to this

- (IBAction)checkboxButton:(UIButton *)button{

     for (UIButton *but in self.arrayButtons) {
         if (but == button)
             [but setImage:selectedImage forState:UIControlStateNormal];
         else
             [but setImage:normalImage forState:UIControlStateNormal];
     }
}

apparently, changing the state of the button will not change its image. At least did not work for me.

Upvotes: 0

Mick MacCallum
Mick MacCallum

Reputation: 130193

I just made a few slight tweaks, and I just tested this and it works.

viewDidLoad: //making sure all buttons have the same selector assigned

[button1 addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
[button3 addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];

Then just slightly modifying the loop to scan for button subviews instead of objects in an arbitrary array (should be more accurate)

- (IBAction)checkboxButton:(UIButton *)button{

    for (UIButton *but in self.view.subviews) {//Change "self.view" to what ever view you are adding these subviews to.
        if (but == button){
            [but setSelected:YES];
        }else{
            [but setSelected:NO];
        }
    }
}

Keep in mind the selected state image/text that you are assigning will not appear until the button is released. When the button is pressed before it is released it is in its highlighted state. This is just a note so you can set this up accordingly.

Upvotes: 0

Manu
Manu

Reputation: 4750

for (UIButton *but in [Your_View subviews]) {

if ([but isKindOfClass:[UIButton class]] && ![but isEqual:button]) {

            [but setSelected:NO];

        }

}

if (!button.selected)  {

        button.selected = !button.selected;
    }

Upvotes: 7

Ravi Raman
Ravi Raman

Reputation: 1070

In order to achieve the desired functionality, you will have to do selection of buttons in the next run loop.

So, call another method from "checkboxButton:".

Do something like,

- (IBAction)checkboxButton:(UIButton *)button
{
    [self performSelector:@selector(highlighButton:) withObject:button afterDelay:0.0];
}

Where "highlightButton:" is defined as:

-(void) highlighButton:(UIButton*)aButton
{
    [aButton setHighlighted:YES];
    for (UIButton* button in self.arrayButtons) {
        if (button != aButton) {
            [button setHighlighted:NO];
        }
    }
}

Upvotes: 0

vishy
vishy

Reputation: 3231

why are you using so much complicated just use below to make selected..

- (IBAction)checkboxButton:(UIButton *)button
{
    button.selected = !button.selected;
}

and make sure your images for button are set in the xib for each state as required.

Upvotes: 0

Related Questions