我正在尝试显示3个单选按钮。如果选择了按钮1,则应禁用其余2个按钮。如果选择了按钮2,则应该禁用按钮1和3。最后,如果选择了按钮3,则应该禁用1和2。我尝试了多种方式,但没有成功。这是我的代码,不胜感激。
@IBAction func button1Clicked(_ sender: UIButton) {
if button2.isSelected {
button2.isSelected = false
button1.isSelected = true
} else {
button1.isSelected = true
}
}
@IBAction func button2Clicked(_ sender: UIButton) {
if button1.isSelected {
button1.isSelected = false
button2.isSelected = true
} else {
button2.isSelected = true
}
}
@IBAction func button3Clicked(_ sender: UIButton) {
if button3.isSelected {
button3.isSelected = false
button2.isSelected = true
//button1.isSelected = true
} else {
button1.isSelected = true
}
}
答案 0 :(得分:0)
您可以通过更改按钮的UIImage来尝试这种方式:
@IBAction func mr(_ sender: Any) {
mrOutlet.setImage(UIImage(named: "check"), for: .normal)
msOutlet.setImage(UIImage(named: "uncheck"), for: .normal)
mrsOutlet.setImage(UIImage(named: "uncheck"), for: .normal)
salutation = "Mr."
}
@IBAction func ms(_ sender: Any) {
msOutlet.setImage(UIImage(named: "check"), for: .normal)
mrOutlet.setImage(UIImage(named: "uncheck"), for: .normal)
mrsOutlet.setImage(UIImage(named: "uncheck"), for: .normal)
salutation = "Ms."
}
@IBAction func mrs(_ sender: Any) {
mrsOutlet.setImage(UIImage(named: "check"), for: .normal)
mrOutlet.setImage(UIImage(named: "uncheck"), for: .normal)
msOutlet.setImage(UIImage(named: "uncheck"), for: .normal)
salutation = "Mrs."
}
希望获得帮助:)