I'm building a Quiz app, and I'm trying to reduce the amount of view controllers by having one view controller act as the Question view controller. This is the view controller where the user is taking the quiz.
I have multiple question banks that are filled with questions specific to a category. These question banks are .swift files, which I believe are classified as classes, they look like this:
import Foundation
class QuestionBank {
var list = [Questions]()
init() {
let item = Questions(text: "what does blah blah blah mean?", correctAnswer: "blah blah", textA: "blah blah blah", textB: "blah", textC: "blah bla", textD: "blee blah" )
list.append(item)
list.append(Questions(text: "this is a question?", correctAnswer: "correct answer", textA: "examplea", textB: "exampleb", textC: "examplec", textD: "exampled"))
list.append(Questions(text: ".......
list.append(Questions(text: ".......
list.append(Questions(text: ".......
}
}
Below is just the first line of the QuestionViewController but it shows that var allQuestions holds GeographyQuestionBank. GeographyQuestionBank looks like the QuestionBank example code above (but with actual questions lol)
import UIKit
import Foundation
class QuestionsViewController: UIViewController {
var allQuestions = GeographyQuestionBank()
...
I understand how to pass things between view controllers by using the prepare(for segue:... ) function. But Im not sure how to pass a class to the allQuestions variable. Is that possible?
I hope this made sense, If not please send a message and I'll try to explain it better. But I just want to be able to pass question bank classes to the QuestionsViewController depending on the category picked on the previous view controller.
It become very simple if you pass the questions array to the Second view Controller like below.
FIRST VIEW CONTROLLER
import UIKit
class FirstViewController: UIViewController {
var selectedCategoryQuestions :[Questions] = []
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func onClickBtnHistoryQuestions(_ sender: Any)
{
//Fill your array with History Question
self.performSegue(withIdentifier: "yourSecondVC", sender: nil)
}
@IBAction func onClickBtnGeoQuestions(_ sender: Any) {
//Fill your array with Geo Question
self.performSegue(withIdentifier: "yourSecondVC", sender: nil)
}
if (segue.identifier == "yourSecondVC")
{
let destVC:yourSecondVC = segue.destination as! yourSecondVC
destVC.questionList = selectedCategoryQuestions
}
}
SECOND VIEW CONTROLLER
class SecondViewController: UIViewController {
var questionList:[Questions] = []
override func viewDidLoad() {
super.viewDidLoad()
println(questionList) //You get selected list here
}
}