gpt4 book ai didi

ios - 单击button1时如何快速更改button2的颜色?

转载 作者:搜寻专家 更新时间:2023-11-01 07:20:01 25 4
gpt4 key购买 nike

我只想在单击 fiveMinButton 时更改 fiveMinButton 和 tenMinButton 的 backgroundColor。为什么这段代码不起作用? @IBAction 也不会起作用。

@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

}

}

最佳答案

您的代码有 2 个问题。

  1. selector语法错误
  2. 您在 viewDidLoad 中添加了按钮操作,这也会出错,因此将该方法写在 viewDidLoad 之外作为类方法。

对于第一个这样的更改选择器。

fiveMinButton.addTarget(self, action: #selector(action(sender:)), for: UIControlEvents.touchUpInside)

对于第二个问题,只需在 viewDidLoad 之外编写操作。

关于ios - 单击button1时如何快速更改button2的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39652721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com