'How can I deselect a button when other consecutive button is pressed?

I'm creating a test that has 5 buttons, each button corresponds to a specific color, the problem is that when I select a consecutive 2nd button, the previous button is still selected, how can I make my code select only one button at a time and deselect the previous one?

How can I fix this?

This is my code

class ViewController: UIViewController {
    
    

    var buttonPressed: Bool = false
    
    @IBOutlet weak var button1: UIButton!
    @IBAction func buttonAction1(_ sender: UIButton) {
        if buttonPressed {
            
            buttonPressed = false
            button1.setImage(UIImage(named: "Bolinha-5"), for: .normal)
        }
        else {
            buttonPressed = true
            button1.setImage(UIImage(named: "Bolinha-4"), for: .normal)
        }
      
            

        
    }
    
    @IBOutlet weak var button2: UIButton!
    @IBAction func buttonAction2(_ sender: UIButton) {
        if buttonPressed {
            
            buttonPressed = false
            button2.setImage(UIImage(named: "Bolinha-5"), for: .normal)
        }
        else {
            buttonPressed = true
            button2.setImage(UIImage(named: "Bolinha-4"), for: .normal)
        }
        
    }

    @IBOutlet weak var button3: UIButton!
    @IBAction func buttonAction3(_ sender: UIButton) {
        if buttonPressed {
            
            buttonPressed = false
            button3.setImage(UIImage(named: "Bolinha-2"), for: .normal)
        }
        else {
            buttonPressed = true
            button3.setImage(UIImage(named: "Bolinha-4"), for: .normal)
        }
        
    }
    
    

    
    @IBOutlet weak var button4: UIButton!
    @IBAction func buttonAction4(_ sender: UIButton) {
        if buttonPressed {
            
            buttonPressed = false
            button4.setImage(UIImage(named: "Bolinha-3"), for: .normal)
        }
        else {
            buttonPressed = true
            button4.setImage(UIImage(named: "Bolinha-4"), for: .normal)
        }
        
    }
   
    
    @IBOutlet weak var button5: UIButton!
    @IBAction func buttonAction5(_ sender: UIButton) {
        if buttonPressed {
            
            buttonPressed = false
            button5.setImage(UIImage(named: "Bolinha-3"), for: .normal)
        }
        else {
            buttonPressed = true
            button5.setImage(UIImage(named: "Bolinha-4"), for: .normal)
        }
        
    }
}


Solution 1:[1]

let's consider you have named your buttons as follows: 1st button: "opt1Button", 2nd button: "opt2Button", 3rd button: "opt3Button" and so on... create an IBAction with name "optionSelected" The function will look like: @IBAction func optionSelected(_ sender: UIButton) {

    opt1Button.isSelected = false
    opt2Button.isSelected = false
    opt3Button.isSelected = false
    opt4Button.isSelected = false
    opt5Button.isSelected = false
    
    sender.isSelected = true
}

As soon any of the options is selected all the buttons will go to 'isSelected' false condition i.e all the buttons will be deselected at first and the selected button will be marked as selected. Same process will be followed again when any of the button is selected, everything will get deselected and the button user has pressed will be marked as selected.

Found this answer from (https://developer.apple.com/forums/thread/685124) and it worked for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leeben J