0

This is the first time i build my own app and i need help, the app is about Quizzes.

The Image of the storyboard

The app has many materials and each material has many Quizzes, This is QuizzesVC

Image of QuizzesVC

And i want to pass data from the QuizzesArray (the code is down below) to QuestionVC

Image of QuestionVC

For example, when the Quiz 1 cell tapped in the math section the QuestionVC should display the first section of the math array:

[Quiz(title: "Quiz1", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)]

QuizzesVC code :

class QuizesVC: UITableViewController {

//...


override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = "Quizes"
    navigationController?.navigationBar.prefersLargeTitles = true
}

// MARK: - Table view data source



override func numberOfSections(in tableView: UITableView) -> Int {
    //...
    }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    //...
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    //...

}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //...
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

}

Here's QuestionVC code:

    class QuestionVC : UIViewController {
    @IBOutlet var questionLabel: UILabel!//QuizzesArray: ques
    @IBOutlet var choiceAButton: UIButton!//QuizzesArray: choiceA
    @IBOutlet var choiceBButton: UIButton!//QuizzesArray: choiceB
    @IBOutlet var choiceCButton: UIButton!//QuizzesArray: choiceC
}

Here's the code of QuizzesArray:

class QuizzesArray {
var mathQuizes = [[Quiz(title: "Quiz1", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
[Quiz(title: "Quiz2", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
[Quiz(title: "Quiz3", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
[Quiz(title: "Quiz4", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
[Quiz(title: "Quiz5", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
[Quiz(title: "Quiz6", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)]
]
var chemistryQuizes = [[Quiz(title: "Quiz1", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
                           [Quiz(title: "Quiz2", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
                           [Quiz(title: "Quiz3", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
                           [Quiz(title: "Quiz4", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
                           [Quiz(title: "Quiz5", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)]
    ]
}
struct Quiz {
var title : String
var ques : String
var choiceA : String
var choiceB : String
var choiceC : String
var correctAnswer : Int
}

Let me know if you need more explanation.

1 Answer 1

2

in didSelectRowAt you can get the index of cell so:

let itemToPass = mathQuizes[indexPath.row]

than perform your segue:

performSegue(withIdentifier: "yourSegue", sender: itemToPass)

the full code I've explained above:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let itemToPass = mathQuizes[indexPath.row]
    performSegue(withIdentifier: "yourSegue", sender: itemToPass)
}

so you have now your item ready to be passed at your next view controller using

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "yourSegue" {
        if let quiz = sender as? Quiz {
           let destination = segue.destination as! NextViewController
           destination.quiz = quiz 
        }
     }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Alstar ,But i am still beginner please can you specify more
they are showing error in this line destination.quiz = quiz // Value of type 'QuestionVC' has no member 'quiz'
of course, that's only an example, you have to set a variable in your nextviewcontroller like this: var quiz: Quiz! than you can use your destination.quiz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.