我正在快速工作。我的屏幕上有多个带有文本的按钮。
每个按钮都有一些对应的标签文本。在多个按钮中,一次应该选择一个。
如果选择了按钮一,则要使用单选按钮更改的按钮一图像和按钮 1 文本应为黑色,其余应为未选择的浅灰色。
我通过以下执行实现了这一点,但是,我正在检查是否有更好的方法。
@IBAction func selectName(_ sender: UIButton) {
let myButtonImage = sender.currentImage
let unSelectedImage = UIImage(named: "un_Checked")
if sender.tag == 1 && myButtonImage!.pngData() == unSelectedImage!.pngData() { //setting for 1st button
sender.setImage(UIImage(named: "checked"), for: UIControl.State.normal )
radioButton4.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButton3.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButton4.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButtonLabel1.textColor = UIColor.darkGray //setting for 1st label
radioButtonLabel2.textColor = UIColor.lightGray
radioButtonLabel3.textColor = UIColor.lightGray
radioButtonLabel4.textColor = UIColor.lightGray
} else if sender.tag == 2 && myButtonImage!.pngData() == unSelectedImage!.pngData() {
sender.setImage(UIImage(named: "checked"), for: UIControl.State.normal ) //setting for 2nd button
radioButton2.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButton3.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButton4.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButtonLabel2.textColor = UIColor.darkGray //setting for 2nd label
radioButtonLabel3.textColor = UIColor.lightGray
radioButtonLabel4.textColor = UIColor.lightGray
radioButtonLabel1.textColor = UIColor.lightGray
} else if sender.tag == 3 && myButtonImage!.pngData() == unSelectedImage!.pngData() {
sender.setImage(UIImage(named: "checked"), for: UIControl.State.normal ) // setting for 3rd button
radioButton3.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButton1.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButton2.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButton4.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButtonLabel3.textColor = UIColor.darkGray //setting for 3rd label
radioButtonLabel1.textColor = UIColor.lightGray
radioButtonLabel2.textColor = UIColor.lightGray
radioButtonLabel4.textColor = UIColor.lightGray
} else if sender.tag == 2 && myButtonImage!.pngData() == unSelectedImage!.pngData() {
radioButton4.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal) //setting for 4th button
radioButton1.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButton2.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButton3.setImage(UIImage(named: "un_Checked"), for: UIControl.State.normal)
radioButtonLabel4.textColor = UIColor.darkGray //setting for 4th label
radioButtonLabel1.textColor = UIColor.lightGray
radioButtonLabel2.textColor = UIColor.lightGray
radioButtonLabel3.textColor = UIColor.lightGray
}
}
但是,默认一个是第一个按钮,应该选择第一个标签。
有什么建议么?
最佳答案
使用当前的实现,您可以使用额外的整数变量进行小重构,以保留选定的按钮标签。
声明一个变量:
var currentSelectedIndex = -1
并更改您的实现,例如:
@IBAction func selectName(_ sender: UIButton) {
if (currentSelectedIndex == sender.tag) {
return
}
currentSelectedIndex = sender.tag
let unSelectedImage = UIImage(named: "un_Checked")
radioButtonLabel1.textColor = UIColor.lightGray
radioButtonLabel2.textColor = UIColor.lightGray
radioButtonLabel3.textColor = UIColor.lightGray
radioButtonLabel4.textColor = UIColor.lightGray
radioButton1.setImage(unSelectedImage, for: UIControl.State.normal)
radioButton2.setImage(unSelectedImage, for: UIControl.State.normal)
radioButton3.setImage(unSelectedImage, for: UIControl.State.normal)
radioButton4.setImage(unSelectedImage, for: UIControl.State.normal)
sender.setImage(UIImage(named: "checked"), for: UIControl.State.normal )
if currentSelectedIndex == 1 {
radioButtonLabel1.textColor = UIColor.darkGray
} else if currentSelectedIndex == 2 {
radioButtonLabel2.textColor = UIColor.darkGray
} else if currentSelectedIndex == 3 {
radioButtonLabel3.textColor = UIColor.darkGray
} else if currentSelectedIndex == 4 {
radioButtonLabel4.textColor = UIColor.darkGray
}
}
备注/建议:
关于ios - 如何在 Swift iOS 中更改单选按钮选定的图像和相应的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61443029/