Change the color of UIButton in IBOutletCollection

后端 未结 2 862
终归单人心
终归单人心 2020-12-20 09:29

I have an IBOutletCollection of UIButton:

@IBOutlet var buttons: [UIButton]!

and a function for a tap:

@IBAction func butto         


        
2条回答
  •  青春惊慌失措
    2020-12-20 09:49

    Try this code:

    @IBOutlet var buttons: [UIButton]!
    var lastTappedButton: UIButton?
    
    @IBAction func button_Tap(_ sender: UIButton) {
    
        for button in buttons {
            if let lastTappedButton = lastTappedButton, lastTappedButton != sender {
                button.backgroundColor = .white
            }
            if button.tag == sender.tag { 
                button.backgroundColor = .blue 
                lastTappedButton = button
            }
        }
    }
    

提交回复
热议问题