我認爲這是使用UIButton
國家有效的方式。 UIButton
有多個狀態,如UIControlState.normal,UIControlState.selected,UIControlState.highlighted
等。 您可以使用UIButton的UIControlState
輕鬆解決您的問題。 首先你需要像這樣設置圖像到你的按鈕。 在viewDidLoad
方法。
// if you have add the "checkbox_off.png and checkbox_on.png" in Images.xcassets then get the image you need to write as below
//UIImage(named : "checkbox_off") no need to add extention like ".png"
btnPrimary.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal)
btnSecondary.setImage(UIImage(named : "checkbox_off.png"), for: UIControlState.normal)
btnNewAdd.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal)
// Selected
btnPrimary.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.selected)
btnSecondary.setImage(UIImage(named : "checkbox_on.png"), for: UIControlState.selected)
btnNewAdd.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.selected)
現在在你的函數只是改變了按鈕的狀態
@IBAction func btnPrimaryAction(_ sender: Any) {
btnPrimary.selected = true
btnSecondary.selected = false
btnNewAdd.selected = false
payType = 1
}
你確定按鈕的狀態。中性?因爲我可以看到你只是爲一個狀態設置圖像。因此,如果狀態是其他任何東西,你的圖像將是空的 –
狀態總是正常的即時消息不改變狀態...它在一個視圖控制器上工作相同的代碼一切都沒有改變,但沒有在其他視圖控制器上工作 –