I have an issue when trying to use buttons to change the color of a label background and button text.
Here is the code I am using:
var player1color = UIColor.red
@IBAction func playerBlue(_ sender: AnyObject)
{
player1color = UIColor.blue
}
...
sender.setTitle!("X", for: UIControlState())
sender.setTitleColor(player1color, for: UIControlState())
...
label.backgroundColor = player1color
label.text = "X"
the program gives no errors before trying to run, but when I try to run the program, it breaks at the beginning " var player1color = UIColor.red " I think I am using the wrong syntax or something, and I am not finding anything that is helping me understand what is wrong here. I have spent a while looking for a solution to this problem to no avail. It works if I simply put "UIColor.red" or any color in place of "player1color" in the label and sender.setTitleColor, but I should be able to use a variable in that place so I can change the color with IBActions etc, right?
Basically I have buttons that represent different preset colors that when clicked set player1color = UIColor.whatever color here so the label background and button text can be the selected colors.
Thank you for the help in advance, and sorry if this is an extremely basic situation. It is hard to find concrete self help for beginners to swift 3.
I am sorry but I didn't get a clear picture of what you are trying to achieve but still hope this would help you for changing the button title you could use this line of code
where
Btn_changeColor
is the name of the buttonand to change the background you could use this for label
this is the syntax for
swift3
hope this helps you.