i just took the course Xcode from udemy and there is a project to create a QuizApp.
Everything works just fine, but i have idea to make a new View Control to give the result of the Quiz. But the code for Quiz is in a struct, so i can't use "instantiateViewController" in Struct to give the result to the ResultViewController.
Can anyone help me?
This is my code from QuestionViewController
import UIKit
class QuestionController: UIViewController {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var progressBar: UIProgressView!
// Button and Score Outlet
@IBOutlet weak var answer1_label: UIButton!
@IBOutlet weak var answer2_Label: UIButton!
@IBOutlet weak var answer3_Label: UIButton!
@IBOutlet weak var scoreLabel: UILabel!
var quizBrain = QuizBrain()
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
@IBAction func answerButtonPressed(_ sender: UIButton) {
let userAnswer = sender.currentTitle!
let userGotItRight = quizBrain.checkAnswer(userAnswer: userAnswer)
if userGotItRight {
sender.backgroundColor = UIColor.green
} else {
sender.backgroundColor = UIColor.red
}
quizBrain.nextQuestion()
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(updateUI), userInfo: nil, repeats: false)
}
@objc func updateUI(){
questionLabel.text = quizBrain.getQuestionText()
//Need to fetch the answers and update the button titles using the setTitle method.
let answerChoices = quizBrain.getAnswers()
answer1_label.setTitle(answerChoices[0], for: .normal)
answer2_Label.setTitle(answerChoices[1], for: .normal)
answer3_Label.setTitle(answerChoices[2], for: .normal)
progressBar.progress = quizBrain.getProgress()
scoreLabel.text = "Score: \(quizBrain.getScore())"
// Reset Button
answer1_label.backgroundColor = UIColor.clear
answer2_Label.backgroundColor = UIColor.clear
answer3_Label.backgroundColor = UIColor.clear
}
}
This is the struct of QuizBrain
import Foundation
struct QuizBrain {
var questionNumber = 0
var score = 0
let quiz = [
Question(q: "Which is the largest organ in the human body?", a: ["Heart", "Skin", "Large Intestine"], correctAnswer: "Skin"),
Question(q: "Five dollars is worth how many nickels?", a: ["25", "50", "100"], correctAnswer: "100"),
Question(q: "What do the letters in the GMT time zone stand for?", a: ["Global Meridian Time", "Greenwich Mean Time", "General Median Time"], correctAnswer: "Greenwich Mean Time"),
Question(q: "What is the French word for 'hat'?", a: ["Chapeau", "Écharpe", "Bonnet"], correctAnswer: "Chapeau"),
Question(q: "In past times, what would a gentleman keep in his fob pocket?", a: ["Notebook", "Handkerchief", "Watch"], correctAnswer: "Watch"),
Question(q: "How would one say goodbye in Spanish?", a: ["Au Revoir", "Adiós", "Salir"], correctAnswer: "Adiós"),
Question(q: "Which of these colours is NOT featured in the logo for Google?", a: ["Green", "Orange", "Blue"], correctAnswer: "Orange"),
Question(q: "What alcoholic drink is made from molasses?", a: ["Rum", "Whisky", "Gin"], correctAnswer: "Rum"),
Question(q: "What type of animal was Harambe?", a: ["Panda", "Gorilla", "Crocodile"], correctAnswer: "Gorilla"),
Question(q: "Where is Tasmania located?", a: ["Indonesia", "Australia", "Scotland"], correctAnswer: "Australia")
]
func getQuestionText() -> String {
return quiz[questionNumber].text
}
//Need a way of fetching the answer choices.
func getAnswers() -> [String] {
return quiz[questionNumber].answers
}
func getProgress() -> Float {
return Float(questionNumber) / Float(quiz.count)
}
mutating func getScore() -> Int {
return score
}
mutating func nextQuestion() {
if questionNumber + 1 < quiz.count {
questionNumber += 1
} else {
questionNumber = 0
}
}
mutating func checkAnswer(userAnswer: String) -> Bool {
//Need to change answer to rightAnswer here.
if userAnswer == quiz[questionNumber].rightAnswer {
score += 1
return true
} else {
return false
}
}
}
i want to show the result in next Control View
And i think, i should change this function
mutating func nextQuestion() {
if questionNumber + 1 < quiz.count {
questionNumber += 1
} else {
questionNumber = 0
}
}
but i don't have any clue, which code i should use to send the result to the ResultView
Can anyone help me?
