I have three buttons named One, Two,
and Three,
and a function buttonPressed
for collection of those three buttons as follows.
var btnTag = [Int]()
@IBAction func buttonPressed(_ sender: UIButton) {
guard let button = sender as UIButton? else { return }
if(btnTag.contains((sender as AnyObject).tag!))
{
if let index = btnTag.index(of: (sender as AnyObject).tag!)
{
btnTag.remove(at: index)
}
}
else
{
btnTag.append((sender as AnyObject).tag!)
}
if !button.isSelected {
button.isSelected = true
button.setTitleColor(.red, for: .normal)
}
else
{
button.isSelected = false
button.setTitleColor(.white, for: .normal)
}
}
I like to change color of button as red
when clicked and then when I click other button the previous button color as white
. So if I press One,
I want One
to be red
and then when I pres Two
, I want Two
to be red
and One
as white
. I did the above but it is allowing multiple selection
and not able to deselect
previous changes. How do I solve this?