swift - 如何将@IBAction Func 引用到几个按钮插座
问题描述
我有很多按钮,按下时会运行非常相似的代码,我目前正在为每个按钮编写一个函数,有没有办法将其压缩为一个函数?
继承人的一些代码:
@IBAction func b0(_ sender: UIButton) {
if pressedArray[0] && buttonsCanBePressed {
pressedArray[0] = false
b0.backgroundColor = notPressedColour
} else if buttonsCanBePressed {
b0.backgroundColor = pressedColour
pressedArray[0] = true
}
}
@IBAction func b1(_ sender: UIButton) {
if pressedArray[1] && buttonsCanBePressed {
pressedArray[1] = false
b1.backgroundColor = notPressedColour
} else if buttonsCanBePressed {
b1.backgroundColor = pressedColour
pressedArray[1] = true
}
}
@IBAction func b2(_ sender: UIButton) {
if pressedArray[2] && buttonsCanBePressed {
pressedArray[2] = false
b2.backgroundColor = notPressedColour
} else if buttonsCanBePressed {
b2.backgroundColor = pressedColour
pressedArray[2] = true
}
}
解决方案
推荐阅读
- python - 我如何知道我的网络摄像头当前是否正常工作?蟒蛇,opencv
- kotlin - 强制为非空而不使用?
- c# - 是否有直接的方法将容器日志从物联网边缘设备发送到 Azure 日志分析工作区?
- winforms - Infragistics UltraToolbarsManager - 将 ApplicationDefaults 添加到 QuickAccessToolbar
- javascript - JavaScript:undefined++的结果是NaN,不是undefined?
- excel - 宏太慢
- ruby-on-rails - Rails,在设计登录存储位置重定向中保留锚标记
- php - 用 php 包含内容
- git - Github 无法从基础分支发出拉取请求
- java - 如何使用 json 对象从上下文处于 mainactivity 的对话框的 editText 发送数据?