Search code examples
swiftxcodevarlet

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


Creates a quiz game

How can I add that when the answer is incorrect, the incorrect answer will turn red and the correct answer will turn green at the same time?

How can I make the colors disappear when the new question comes? I have it that when you press an answer, a new question will come right after that

EDIT: This code is working fine.

@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 {
        $0.layer.cornerRadius = 20
        $0.backgroundColor = UIColor.orange
        $0.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: { $0.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 {                
        $0.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....
}

}


Solution

  • Instead of having four IBOutlets use IBOutletCollection and connect these four buttons to this collection.

    In answerPressed method if the correct answer is selected change clicked button color to green. If the wrong answer is selected change selected answer color to red, then get the correct answer button from the collection and change its color to green. After 5 seconds reload next question.

    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 {
                $0.layer.cornerRadius = 20
                $0.backgroundColor = UIColor.orange
                $0.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: { $0.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()
            }        
        }
    }
    

    Change all buttons color in oppdaterSpm method

    func oppdaterSpm() {
        if Number <= allQuestions.list.count - 1{
            questionLabel.text = allQuestions.list[Number].question
            options.forEach {                
                $0.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....
        }
    }