Button Color Change Based on User Selection

I would like the background color of the buttons to change to either red or green depending on whether or not the correct answer is selected within my quiz app (Green for Correct Answer or Red for Incorrect). If the user selects the wrong answer, I want the correct answer button to appear in Green and the incorrect button that was selected to appear in Red. If the user selects the correct answer, I would like the button background color of the correct answer that was selected to appear in Green only (no other button background should change color). I am unsure of the exact syntax and/or place to add the correct code.


Below are 3 areas of code in my quiz app. Currently using xCode 10.1 - Thanks for any help that can be provided.


Question.swift


import Foundation


class Question {

let questionImage: String

let question: String

let optionA: String

let optionB: String

let optionC: String

let optionD: String

let correctAnswer: Int

init(image: String, questionText: String, choiceA: String, choiceB: String, choiceC: String, choiceD: String, answer: Int) {

questionImage = image

question = questionText

optionA = choiceA

optionB = choiceB

optionC = choiceC

optionD = choiceD

correctAnswer = answer

}

}


Or within the QuestionBank.swift


import Foundation


class QuestionBank{

var list = [Question]()

init() {

list.append(Question(image: "continent2", questionText: " Where is the Empire State Building? ", choiceA: "A. Europe", choiceB: "B. Asia", choiceC: "C. North America", choiceD: "D. Australia", answer: 3))



Or within ViewController.swift


@IBAction func answerPressed(_ sender: UIButton) {

if sender.tag == selectedAnswer {

print("correct")

score += 1

}else{

print("wrong")

}

questionNumber += 1

updateQuestion()

Button Color Change Based on User Selection
 
 
Q