Deselect all UIButtons when one is selected

Get the parent view of the current button and iterate through all the buttons inside, unselecting all of them. Then, select the current one.

// Unselect all the buttons in the parent view
for (UIView *button in currentButton.superview.subviews) {
    if ([button isKindOfClass:[UIButton class]]) {
        [(UIButton *)button setSelected:NO];
    }
}

// Set the current button as the only selected one
[currentButton setSelected:YES];

Note: As suggested on the comments, you could keep an array of buttons and go over it the same way the above code does with the subviews of the parent view. This will improve the performance of your code in case the view containing the buttons has many other subviews inside.


I know its too late to answer this question but I did it in only small lines of code . Here is what i did :

NSArray *arrView = self.view.subviews;
    for (UIButton *button in arrView) {
        if ([button isKindOfClass:[UIButton class]]) {
            [((UIButton *) button) setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
[button1 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

Simple way to do.

-(void)buttonSelected:(id)sender{
    UIButton *currentButton = (UIButton *)sender;
    for(UIView *view in self.view.subviews){

        if([view isKindOfClass:[UIButton class]]){

            UIButton *btn = (UIButton *)view;
            [btn setSelected:NO];
        }
    }

    [currentButton setSelected:YES];

}