现在我有一个紫色和红色的分数。我想使用1和2按钮添加颜色的分数。我知道如何通过为特定颜色创建一组按钮来做到这一点,但一次又一次地这样做会产生问题。所以我要做的是如果选择紫色,当且仅当按下1时,紫色的总分将= 1.我想我正在尝试的是为按钮执行switch语句。
import UIKit
class ViewController: UIViewController {
var purpleScore = 0
var redScore = 0
@IBOutlet var plus1: UIButton!
@IBOutlet var plus2: UIButton!
@IBOutlet var addRed: UIButton!
@IBOutlet var addPurlple: UIButton!
@IBOutlet var totalScorePurple: UILabel!
@IBOutlet var totalScoreRed: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
plus1.isHidden = true
plus2.isHidden = true
}
@IBAction func enteringRed(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
@IBAction func enteringPurple(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
@IBAction func plus1(_ sender: Any) {
}
@IBAction func plus2(_ sender: Any) {
}
}
答案 0 :(得分:0)
如果我正确理解为上述评论,那么这应该有效
为红色和紫色创建2个Bool以查看是否检查天气,这是最简单的方法,否则您只需检查redButton颜色是否已更改(如果有)或.selected = true
var redChecked = false
var purpleChecked = false
@IBAction func enteringRed(_ sender: Any) {
redChecked = !redChecked
//change color etc..
}
@IBAction func enteringPurple(_ sender: Any) {
purpleChecked = !purpleChecked
//change color etc..
}
@IBAction func plus1(_ sender: Any) {
if redChecked {
redScore+=1
}
if purpleChecked {
purpleScore+=1
}
}
@IBAction func plus2(_ sender: Any) {
if redChecked {
redScore+=2
}
if purpleChecked {
purpleScore+=2
}
}