简体   繁体   中英

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?

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

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM