我有很多按钮,当按下这些按钮时,它们会运行非常相似的代码,目前我正在为每个按钮编写一个函数,有没有办法将其压缩为一个函数?
这里有一些代码:
@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
}
}