Я пытаюсь изменить backgroundColor
для кнопки на кране. Когда кнопка нажата, она должна изменить backgroundColor
. Но моя проблема в том, что мне нужно два крана для этого события. И я не понимаю, почему.
Мой код:
for (index, cue) in cuestionesCC.enumerated(){
let buttonYes = UIButton()
buttonYes.backgroundColor = .green
buttonYes.titleEdgeInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10) // Add padding around text
buttonYes.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
buttonYes.tag = 1
buttonYes.setTitle("Yes", for: .normal)
buttonYes.addTarget(self, action: #selector(buttonActionYesNo), for: .touchUpInside)
let buttonNo = UIButton()
buttonNo.backgroundColor = .red
buttonNo.titleEdgeInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10) // Add padding around text
buttonNo.contentEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
buttonNo.tag = 2
buttonNo.setTitle("No", for: .normal)
buttonNo.addTarget(self, action: #selector(buttonActionYesNo), for: .touchUpInside)
stackViewH.addArrangedSubview(buttonYes)
stackViewH.addArrangedSubview(buttonNo)
}
@objc func buttonActionYesNo(sender: UIButton!) {
print (sender.tag)
sender.backgroundColor = .gray
}
Когда я нажимаю кнопку один раз, она печатает тег. Но мне нужно еще одно нажатие дополнительно для изменения backgroundColor
кнопки. Я не понимаю этой логики.