Tameem
Tameem

Reputation: 21

I need to pass data from UITableViewController to UIViewController using Array

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.

Upvotes: 0

Views: 137

Answers (1)

Alastar
Alastar

Reputation: 1374

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 
        }
     }
}

Upvotes: 2

Related Questions