Selecting and canceling multiple buttons

1. The button selection cannot be canceled

   btn.layer.borderColor = btn.selected?Color_Theme.CGColor:Color_Btn_Border.CGColor;
    //单选
    if (btn.selected) {
        for (UIButton *btnTemp in self.contentView.subviews) {
            if ([btnTemp isKindOfClass:[UIButton class]] && ![btnTemp isEqual:btn]) {
                    btnTemp.selected = NO;
                    btnTemp.layer.borderColor = Color_Btn_Border.CGColor;
            }
        }
    }
    

2. Click the button to select, click again to deselect.

btn.selected = !btn.selected;
 btn.layer.borderColor = btn.selected?Color_Theme.CGColor:Color_Btn_Border.CGColor;
    //单选
    if (btn.selected) {
        for (UIButton *btnTemp in self.contentView.subviews) {
            if ([btnTemp isKindOfClass:[UIButton class]] && ![btnTemp isEqual:btn]) {
                    btnTemp.selected = NO;
                    btnTemp.layer.borderColor = Color_Btn_Border.CGColor;
            }
        }
    }

Guess you like

Origin blog.csdn.net/weixin_42050662/article/details/122733721