Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display color on different buttons

So I am making a quiz app, and I am using the following code to display to the user if the answer is correct or wrong.

The app looks like this:
Image here

This is the code to display:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@IBAction func checkAnswer(_ sender: UIButton) {
        if let question = currentQuestion, let answer = sender.titleLabel?.text {
            
            if (question.validateAnswer(to: answer)) {
                score.incrementCorrectAnswers()
                currentScore = currentScore + 1
                feedbackLabel.textColor = UIColor(red:0.15, green:0.61, blue:0.61, alpha:1.0)
                feedbackLabel.text = "Correct!"
                scoreLabel.text = "\(currentScore)"
                
                sender.backgroundColor = hexStringToUIColor(hex: "213F4B")
            } else {
                score.incrementIncorrectAnswers()
                feedbackLabel.textColor = UIColor(red:0.82, green:0.40, blue:0.26, alpha:1.0)
                feedbackLabel.text = "Wrong, correct answer is: \n\(currentQuestion!.getAnswer())"
                
                sender.shake()
                
                sender.backgroundColor = hexStringToUIColor(hex: "3F2F4B")
                
            }
            answer1Button.isEnabled = false
            answer2Button.isEnabled = false
            answer3Button.isEnabled = false
            answer4Button.isEnabled = false
            nextQuestionButton.isEnabled = true
            
            feedbackLabel.isHidden = false
        }
    }

When using this, if the user tap the wrong button, the button turns red, and if the user taps the correct button, the button turns green. How can I make it so if the user taps the wrong button, the answer on the correct button turns green at the same time as the wrong one goes red? Let me know if any other code from project is necessarily.

>Solution :

You could loop through all UIButton‘s and check for sender.titleLabel?.text like this:

for view in self.view.subviews as [UIView] {
    if let btn = view as? UIButton {
        if btn.titleLabel?.text == currentQuestion!.getAnswer() {
            btn.backgroundColor = hexStringToUIColor(hex: "213F4B") // Green Color
        }
    }
}

I’m not on my computer so won’t be able to test it, so try it out and let me know how it works.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading