Change Button Properties when Tapped?

Hi!

I have a UI with multiple buttons, and I need the button that is tapped to change its background colour to red. Is there a way to do this as a function, something like (but obviously not):

buttonTapped.backgroundColor = UIColor.red

Please help!

Tom

Hi Tom,

Welcome to the Code Crew community.

If you have the buttons grouped such that when any one of them are tapped they call the same IBAction code then the way to discriminate between the different buttons is to give each one a different tag value in storyboard. Make sure that your IBAction sender: parameter is defined as UIButton rather than Any.

Then in your IBAction code you can set sender.backgroundColor = UIColor.red or you can use a switch statement on sender.tag and for a given tag value set a different color.