多个按钮到单个 IBAction - 如何更改上一个点击按钮的背景?
Multiple buttons to single IBAction - How to change background of the previous tapped button?
我有一组用户可以点击的按钮,几乎就像一个计算器。
该应用将收集一系列的 2 个按钮点击。这是 select 牌组中的一张牌。第一次点击将是王牌到王牌。第二次点击将是一套西装。
点击第一个按钮时,背景会变为黄色(等级)。
当点击第二个按钮时,它会将 2 个 select 离子保存为一对并附加到一个字符串(花色)。
点击第二个按钮后,我想将第一个点击的按钮改回蓝色。
我所有的按钮都链接到一个 IBAction 'buttonTapped',我无法在点击第二个按钮时将第一个按钮的背景更改为蓝色。 (我什至可以接受第二个按钮将链接到此 IBAction 的所有按钮更改为蓝色)
@IBAction func buttonTapped(theButton: UIButton) {
var buttonDump = theButton.titleLabel!.text!
var firstChar = Array(buttonDump)[0]
if firstChar == "♠️" || firstChar == "♥️" || firstChar == "♦️" || firstChar == "♣️" {
// MUST BE THE 2nd TAP, SO LETS CHANGE THE 1st TAPPED BG BACK TO BLUE
self.n2.text = buttonDump
theButton.backgroundColor = UIColor.blueColor()
} else {
self.n1.text = buttonDump
theButton.backgroundColor = UIColor.yellowColor()
}
theButton
参数是用户点击的按钮。基本上是将背景更改为黄色的按钮,您需要保留对该按钮的引用,然后在点击第二个按钮时将其设置为蓝色。
像这样:
var firstButton: UIButton?
@IBAction func buttonTapped(theButton: UIButton) {
var buttonDump = theButton.titleLabel!.text!
var firstChar = Array(buttonDump)[0]
if firstChar == "♠️" || firstChar == "♥️" || firstChar == "♦️" || firstChar == "♣️" {
// MUST BE THE 2nd TAP, SO LETS CHANGE THE 1st TAPPED BG BACK TO BLUE
//self.n2.text = buttonDump
// Change first button to blue
if( self.firstButton != nil ) {
self.firstButton!.backgroundColor = UIColor.blueColor()
}
} else {
//self.n1.text = buttonDump
// Keep reference to first button
self.firstButton = theButton
self.firstButton!.backgroundColor = UIColor.yellowColor()
}
}
我有一组用户可以点击的按钮,几乎就像一个计算器。
该应用将收集一系列的 2 个按钮点击。这是 select 牌组中的一张牌。第一次点击将是王牌到王牌。第二次点击将是一套西装。
点击第一个按钮时,背景会变为黄色(等级)。 当点击第二个按钮时,它会将 2 个 select 离子保存为一对并附加到一个字符串(花色)。
点击第二个按钮后,我想将第一个点击的按钮改回蓝色。
我所有的按钮都链接到一个 IBAction 'buttonTapped',我无法在点击第二个按钮时将第一个按钮的背景更改为蓝色。 (我什至可以接受第二个按钮将链接到此 IBAction 的所有按钮更改为蓝色)
@IBAction func buttonTapped(theButton: UIButton) {
var buttonDump = theButton.titleLabel!.text!
var firstChar = Array(buttonDump)[0]
if firstChar == "♠️" || firstChar == "♥️" || firstChar == "♦️" || firstChar == "♣️" {
// MUST BE THE 2nd TAP, SO LETS CHANGE THE 1st TAPPED BG BACK TO BLUE
self.n2.text = buttonDump
theButton.backgroundColor = UIColor.blueColor()
} else {
self.n1.text = buttonDump
theButton.backgroundColor = UIColor.yellowColor()
}
theButton
参数是用户点击的按钮。基本上是将背景更改为黄色的按钮,您需要保留对该按钮的引用,然后在点击第二个按钮时将其设置为蓝色。
像这样:
var firstButton: UIButton?
@IBAction func buttonTapped(theButton: UIButton) {
var buttonDump = theButton.titleLabel!.text!
var firstChar = Array(buttonDump)[0]
if firstChar == "♠️" || firstChar == "♥️" || firstChar == "♦️" || firstChar == "♣️" {
// MUST BE THE 2nd TAP, SO LETS CHANGE THE 1st TAPPED BG BACK TO BLUE
//self.n2.text = buttonDump
// Change first button to blue
if( self.firstButton != nil ) {
self.firstButton!.backgroundColor = UIColor.blueColor()
}
} else {
//self.n1.text = buttonDump
// Keep reference to first button
self.firstButton = theButton
self.firstButton!.backgroundColor = UIColor.yellowColor()
}
}