All I want to do is change the backgroundColor
of fiveMinButton and tenMinButton when fiveMinButton is clicked. Why doesn't this code work? @IBAction
will not work either.
@IBOutlet weak var fiveMinButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
fiveMinButton.addTarget(self, action: Selector(("action:")), for: UIControlEvents.touchUpInside)
func action(sender: UIButton){
if sender === fiveMinButton {
fiveMinButton.backgroundColor = UIColor.gray
tenMinButton.backgroundColor = UIColor.lightgray
}
}
You are writing the action method inside the viewDidload, Try this:
If button type rounded rect ..Color cannot be applied. So set the button to type custom and good to go
or you can set the background image or image to the button to gain the color
or
There is 2 problems with your code.
selector
is wrongviewDidLoad
that will also wrong, so write that method outside theviewDidLoad
as class method.For first one change selector like this.
For second problem just write action out side the
viewDidLoad
.