如何在同一操作中设置按钮的图像?

时间:2019-08-01 08:51:31

标签: swift

在我的ViewController中,

有多个按钮连接到同一操作

我的按钮就像选中或取消选中

我在下面尝试过

我的动作被触发,但是图像没有改变

var uncheckBoxImage = UIImage(named: "uncheckBox")
var checkBoxImage = UIImage(named: "checkBox") 

override func viewDidLoad() {
        super.viewDidLoad()
        for button in buttons{
            button?.setImage(uncheckBoxImage, for: .normal) 
            //every button I set uncheckBoxImage first.
        }
    }

@IBAction func btnAction(_ sender: UIButton) { //connect every button
        if sender.imageView == checkBoxImage {
            sender.setImage(uncheckBoxImage, for: .normal)
        }else {
             sender.setImage(checkBoxImage, for: .normal)
        }
}

我也在下面尝试过,但是它不能处理多个按钮

var cube:Bool = false     
@IBAction func btnAction(_ sender: Any) {
 cube = !cube
 if cube {
    sender.setImage(checkBoxImage, for: .normal)
        } else {
            sender.setImage(uncheckBoxImage, for: .normal)
        }
    }

我应该如何解决这些情况?

就像它对您有一些问题

您只需要单击以选中它,或者再次单击以取消选中

5 个答案:

答案 0 :(得分:1)

诀窍是对按钮使用selectedState。 将两个图像都设置为.normal.selected状态:

button?.setImage(uncheckBoxImage, for: .normal)
button?.setImage(checkBoxImage, for: .selected) 

然后,在IBAction中:

@IBAction func btnAction(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
    ...
}

答案 1 :(得分:0)

对于两个状态,即抽象“正常”和“选定”,最好使用上述答案,但是如果需要处理多种情况,则可以使用<#UIButton#>.currentImage!.isEqual(to: foo)compare images和仅使用.normal状态。

这是一个示例:

@IBAction func btnAction(_ sender: UIButton) {

    if sender.currentImage != nil && sender.currentImage!.isEqual(UIImage(named: "a")) {
        sender.setImage(UIImage(named: "b"), for: .normal)
    } else {
        sender.setImage(UIImage(named: "a"), for: .normal)
    }
}

答案 2 :(得分:0)

您应该实现其他按钮的状态,它将自动更改图像

    var uncheckBoxImage = UIImage(named: "uncheckBox")
var checkBoxImage = UIImage(named: "checkBox")

override func viewDidLoad() {
    super.viewDidLoad()
    for button in buttons{
        button?.setImage(uncheckBoxImage, for: .normal)
        button?.setImage(checkBoxImage, for: .selected)
    }
}

@IBAction func btnAction(_ sender: UIButton) { //connect every button
    sender.isSelected = !sender.isSelected
}

答案 3 :(得分:0)

嗨,请尝试使用淡色

var uncheckBoxImage = UIImage(named: "uncheckBox")
var checkBoxImage = UIImage(named: "checkBox") 

override func viewDidLoad() {
    super.viewDidLoad()
    for button in buttons{
        button?.setImage(uncheckBoxImage, for: .normal) 
        button?.tintColor = UIColor.clear
    }
}

@IBAction func btnAction(_ sender: UIButton) { 
     if sender.tintColor == UIColor.clear {
        sender.setImage(checkBoxImage, for: .normal)
        sender.tintColor = UIColor.green
    }else if sender.tintColor == UIColor.green {
        sender.setImage(uncheckBoxImage, for: .normal)
        sender.tintColor = UIColor.clear
    }
}

答案 4 :(得分:0)

如果您要选择多个选项,则可以使用。

override func viewDidLoad() {
    super.viewDidLoad()
     for button in buttons {
        button.setImage(UIImage(named: "uncheckBox"), for: .normal)
        button.setImage(UIImage(named: "checkBox"), for: .selected)
        button.tintColor = .clear
    }
}

@IBAction func btnAction(_ sender: UIButton) { 
// toggle automatic changes selection state
            sender.isSelected.toggle()

}