I have 4 custom-made toggle buttons. Users can select multiple buttons and toggle the same button, however, there must always be at least 1 selected button.
If 1 out of 4 buttons are selected, then the selected button can not be toggled to become deselected.
I am looking for a way to determine this logic. My thought was to simply count the selected items in the button array, and if there is only 1 selected, then escape via return.
Here's my progress
func toggleButton(button: ToggleButton){
let buttonArray = [btn1, btn2, btn3, btn4]
for btn in buttonArray{
// Count the amount of selected buttons in buttonArray
// if 1, return.
}
button.isSelected = !button.isSelected
}
Is there a way to count the amount of selected buttons in buttonArray?
Maybe a variable numberOfSelected
to keep track of the number of selected buttons? And if it's equal to 1, return:
var numberOfSelected = 0
let buttonArray = [btn1, btn2, btn3, btn4]
for btn in buttonArray {
if btn.isSelected {
numberOfSelected += 1
}
}
if numberOfSelected == 1 { return } /// escape via return