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)
}
}
}
2
Answers
What you are going to accomplish is called Radio Buttons, unfortunately iOS (unlike macOS) doesn’t provide this functionality.
My suggestion takes advantage of the option to assign different images to different states in Interface Builder – in this case the
Default
andSelected
state – and to create an outlet collection, an array representing a sequence of UI elements of the same type.The suggestion doesn’t support an empty selection, by default the first button is selected.
In ViewController
create an outlet collection
and one
IBAction
In Interface Buider
Default
andSelected
states of each button in the Attribute Inspector.In ViewController
create a property for the tag of the currently selected button
in viewDidLoad select the first button
Complete the IBAction, it deselects the previous button and selects the current.
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) {
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.