Let say I have 10 UIButton
like below
I want to implement single selection but I have no idea how to do this. The condition is like below :
At first, the ALL
button will in selected position(highlight or etc). When I click another button like VCU
button, the VCU
button will highlight And the ALL
button will in normal state(unhighlight).
Snippet code :
@IBAction func buttonDisplayAction(_ sender: UIButton) {
if sender == self.buttonAll {
self.currNr = -1
self.filteredDataRoomStatus = self.dataRoomStatus
} else if sender == self.buttonVCU {
self.currNr = 1
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonOC {
self.currNr = 5
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonVCC {
self.currNr = 0
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonVD {
self.currNr = 2
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "checkout zistatus", searchStr: "0 2", equalFlag: false)
} else if sender == self.buttonED {
self.currNr = 3
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonOD {
self.currNr = 4
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonCO {
self.currNr = 12
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "checkout", searchStr: "1", equalFlag: true)
} else if sender == self.buttonDnD {
self.currNr = 8
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonOOO {
self.currNr = 6
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
}
do {
let currButton: UIButton = (sender as UIButton)
currButton.isSelected = !currButton.isSelected
if currButton.isSelected {
currButton.backgroundColor = UIColor.darkGray
} else {
currButton.backgroundColor = UIColor.clear
}
}
}
Code above make the multiple selection but I need a single selection. I have read this and this but I am still stuck.