Hi! My first app, quiz app, is almost ready. I just have a few minor problems.
-
I can't set the duration of the quiz via buttons (how many questions, I mean). How can I solve this problem? Some code:
ViewController.swift
var numberOfQuestions = Int()// Buttons
@IBAction func Vragen20( _ sender: Any ) {let vc = storyboard?.instantiateViewController(identifier: "game") as ! GameViewController
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true )
numberOfQuestions = 20
}
@IBAction func Vragen50( _ sender: Any ) {
let vc = storyboard?.instantiateViewController(identifier: "game") as ! GameViewController
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true )
numberOfQuestions = 50
}
@IBAction func Vragen100( _ sender: Any ) {
let vc = storyboard?.instantiateViewController(identifier: "game") as ! GameViewController
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true )
numberOfQuestions = 100
}
GameViewController.swift
// Here I need the variable numberOfQuestions
if let index = allQuestions.firstIndex(where: { $0.text == question.text }) {
if Progress < 100 {
// Next question
let NextQuestion = allQuestions.randomElement()
print("(NextQuestion!.text)")
CurrentQuestion = nil
ConfiqureUI(question:NextQuestion!)
Progress += 1
updateLabel()
}
else {
let vc = storyboard?.instantiateViewController(identifier: "end") as! ViewController
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true)
}
}
- How can I make it so that the same question never appears twice? (I have enough questions to do that) Some code:
struct Question {
let text: String
let answers: [Answer]
}
struct Answer {
let text: String
let correct: Bool
}
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
updateLabel()
ConfiqureUI(question: allQuestions.randomElement()!)
}
Hopefully someone can help me!