How to change Color of button2 in swift when button1 is clicked?

后端 未结 3 1257
礼貌的吻别
礼貌的吻别 2021-01-24 01:50

All I want to do is change the backgroundColor of fiveMinButton and tenMinButton when fiveMinButton is clicked. Why doesn\'t this code work? @IBAction

3条回答
  •  梦谈多话
    2021-01-24 02:32

    You are writing the action method inside the viewDidload, Try this:

    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
            }
    }
    

提交回复
热议问题