编辑 - 我如何添加当答案错误时它会在正确答案中变成红色和绿色? Swift 4.2

Edit- How can I add that when the answer is wrong it will turn red and green in the correct answer? Swift 4.2

创建问答游戏

如何添加当答案不正确时,错误答案会变成红色,同时正确答案会变成绿色?

新题来了怎么让颜色消失?我知道当你按下一个答案时,一个新的问题会马上出现

编辑:此代码运行良好。

@IBOutlet var options: [UIButton]!
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var progressView: UIView!

var allQuestions = QuestionBank()
var Number: Int = 0
var selectedAnswer: Int = 0

override func viewDidLoad() {
    super.viewDidLoad()
    oppdatertekst()
    options.forEach {
        [=11=].layer.cornerRadius = 20
        [=11=].backgroundColor = UIColor.orange
        [=11=].setTitleColor(UIColor.black, for: .normal)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func answerPressed(_ sender: UIButton) {
    feedback()
    if sender.tag == selectedAnswer {
        sender.backgroundColor = UIColor.green
        let riktig = NSLocalizedString("Quiz.riktig", comment: "")
        ProgressHUD.showSuccess(riktig)
    } else if let correctOption = options.first(where: { [=11=].tag == selectedAnswer }) {
        let feilnr = NSLocalizedString("Quiz.feilnr", comment: "")
        ProgressHUD.showError("\(feilnr)\(selectedAnswer)")
        correctOption.backgroundColor = UIColor.green
        sender.backgroundColor = UIColor.red
    }
    DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
        self.Number += 1
        self.oppdatertekst()
    }
}

func oppdaterSpm() {
if Number <= allQuestions.list.count - 1{
    questionLabel.text = allQuestions.list[Number].question
    options.forEach {                
        [=11=].backgroundColor = .white
    }
    options[0].setTitle(allQuestions.list[Number].optionA, for: .normal)
    options[1].setTitle(allQuestions.list[Number].optionB, for: .normal)
    options[2].setTitle(allQuestions.list[Number].optionC, for: .normal)
    options[3].setTitle(allQuestions.list[Number].optionD, for: .normal)
    selectedAnswer = allQuestions.list[Number].correctAnswer
} else {
    let alert....
}

}

不要使用四个 IBOutlet,而是使用 IBOutletCollection 并将这四个按钮连接到此集合。

answerPressed 方法中,如果选择了正确答案,则将单击的按钮颜色更改为绿色。如果选择了错误的答案,则将所选答案颜色更改为红色,然后从集合中获取正确答案按钮并将其颜色更改为绿色。 5 秒后重新加载下一个问题。

class ViewController: UIViewController {

    @IBOutlet var options: [UIButton]!
    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var progressView: UIView!

    var allQuestions = QuestionBank()
    var Number: Int = 0
    var selectedAnswer: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        oppdatertekst()
        options.forEach {
            [=10=].layer.cornerRadius = 20
            [=10=].backgroundColor = UIColor.orange
            [=10=].setTitleColor(UIColor.black, for: .normal)
        }
    }
    @IBAction func answerPressed(_ sender: UIButton) {
        feedback()
        if sender.tag == selectedAnswer {
            sender.backgroundColor = UIColor.green
            let riktig = NSLocalizedString("Quiz.riktig", comment: "")
            ProgressHUD.showSuccess(riktig)
        } else if let correctOption = options.first(where: { [=10=].tag == selectedAnswer }) {
            let feilnr = NSLocalizedString("Quiz.feilnr", comment: "")
            ProgressHUD.showError("\(feilnr)\(selectedAnswer)")
            correctOption.backgroundColor = UIColor.green
            sender.backgroundColor = UIColor.red
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
            Number += 1
            oppdatertekst()
        }        
    }
}

更改 oppdaterSpm 方法中的所有按钮颜色

func oppdaterSpm() {
    if Number <= allQuestions.list.count - 1{
        questionLabel.text = allQuestions.list[Number].question
        options.forEach {                
            [=11=].backgroundColor = .white
        }
        options[0].setTitle(allQuestions.list[Number].optionA, for: .normal)
        options[1].setTitle(allQuestions.list[Number].optionB, for: .normal)
        options[2].setTitle(allQuestions.list[Number].optionC, for: .normal)
        options[3].setTitle(allQuestions.list[Number].optionD, for: .normal)
        selectedAnswer = allQuestions.list[Number].correctAnswer
    } else {
        let alert....
    }
}

您必须在常用方法中编写 UIButton 更改。在任何地方调用它。

当新问题出现时:

func whenNewQuestionComes() {

    optionA.layer.cornerRadius = 20
    optionA.backgroundColor = UIColor.orange
    optionA.setTitleColor(UIColor.black, for: .normal)
    optionB.layer.cornerRadius = 20
    optionB.backgroundColor = UIColor.orange
    optionB.setTitleColor(UIColor.black, for: .normal)
    optionC.layer.cornerRadius = 20
    optionC.backgroundColor = UIColor.orange
    optionC.setTitleColor(UIColor.black, for: .normal)
    optionD.layer.cornerRadius = 20
    optionD.backgroundColor = UIColor.orange
    optionD.setTitleColor(UIColor.black, for: .normal)

}

显示绿色和红色

@IBAction func answerPressed(_ sender: UIButton) {
    feedback()

    optionA.backgroundColor = UIColor.red
    optionB.backgroundColor = UIColor.red
    optionC.backgroundColor = UIColor.red
    optionD.backgroundColor = UIColor.red

    if sender.tag == selectedAnswer {
        sender.backgroundColor = UIColor.green
        let riktig = NSLocalizedString("Quiz.riktig", comment: "")
        ProgressHUD.showSuccess(riktig)
    } 
    /*
    else if let correctOption = options.first(where: { [=11=].tag == selectedAnswer }) {
        let feilnr = NSLocalizedString("Quiz.feilnr", comment: "")
        ProgressHUD.showError("\(feilnr)\(selectedAnswer)")
        correctOption.backgroundColor = UIColor.green
        sender.backgroundColor = UIColor.red
    }
    */
    DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
        self.Number += 1
        self.oppdatertekst()
    }
}