简体   繁体   中英

Select / deselect UIButton when tapped

I have a simple question which I cannot seem to find an answer for.

I have several UIButton s which are stored in an NSArray , with a for loop to set button.selected = YES when they are tapped. I need to deselect the same button when it is tapped, but I can't seem to find anything online to help. Here is my code:

- (IBAction)buttonPressed:(UIButton *)sender {
    NSArray *buttons = [NSArray arrayWithObjects:_asbBtn, _vwfBtn, _bpBtn, _rtaBtn, _mslmBtn, _pbaBtn, _rcfBtn, _mspBtn, _wpBtn, _aawBtn, _ppiBtn, _convBtn, nil];

    // Select buttons
    for (UIButton *button in buttons) {
        if (button == sender) {
            button.selected = YES;
        }
    }
}

Maybe this is not the best approach, sorry if I am missing something simple. I have tried adding else { button.selected = NO; but this only allows one button to be selected and deselects all others. Please can someone guide me in the right direction to deselect the current selected button when tapped.

If you want to toggle between selected and unselected states each time you click on a UIButton instance, you can use the Objective-C code below:

- (IBAction) buttonPressed:(id)sender {
    if ([sender isSelected]) {
        [sender setSelected: NO];
    } else {
        [sender setSelected: YES];
    }
}

Note that you can have the same result with an even shorter implementation:

- (IBAction) buttonPressed:(id)sender {
    [sender setSelected: ![sender isSelected]];
}

With Swift 3, you would use the following code:

@IBAction func buttonPressed(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
}

Why are you even doing it that way? That is so overly complicated. The simple way is to do this

- (IBAction)reasonTapped:(id)sender 
{
    // Personal preference here but I would check to make sure
    // that the sender is actually a button.
    if([sender isKindOfClass:[UIButton class]]) {
        // Sender will be the button that is pressed.
        if([sender isSelected]) {
            // If already selected then set to selected to NO
            [sender setSelected:NO];
        } else {
            // Else set to selected to YES
            [sender setSelected:YES];
        } 
    } else {
        // Else if not an instance of UIButton throw some sort of warning.
    }
}

If you have linked all your buttons up correctly either in your code or in interface/storyboards when the user presses a button it will call this method and sender will be that button that is pressed. So no need for the redundant array at the beginning.

bind all button with this single IBAction

- (IBAction)buttonPressed:(UIButton *)sender 
{
    if ([sender isSelected]) {
        [sender setSelected:NO];
    }
    else
    {
        [sender setSelected:YES];
    } 
}

Try this code:

 -(IBAction)reasonTapped:(id)sender{
           if ([sender isSelected]) {
        [sender setSelected: NO];
    } else {
        [sender setSelected: YES];
    }
   }

I figured out a pretty easy way to solve this. My example is for 2 buttons but you can easily add more if statements for additional buttons. Connect all buttons to the .h file as properties and name them (I did button1 & button2). Place the following code in your .m file and Connect it (via the storyboard) to all of your buttons. Make sure when you are setting up your button to set an image for BOTH the normal UIControlStateNormal & UIControlStateSelected or this wont work.

- (IBAction)selectedButton1:(id)sender {

if ([sender isSelected]) {
    [sender setSelected:NO];

    if (sender == self.button1) {
        [self.button2 setSelected:YES];
    }
    if (sender == self.button2) {
        [self.button1 setSelected:YES];
    }
}

else
{
    [sender setSelected:YES];

    if (sender == self.button1) {
        [self.button2 setSelected:NO];
    }
    if (sender == self.button2) {
        [self.button1 setSelected:NO];
    }

}

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