这是我第一次构建自己的应用程序并且需要帮助,该应用程序与Quizzes有关。
该应用程序包含许多材料,每种材料都有许多Quizzes,这是 QuizzesVC
我想从 QuizzesArray 传递数据(下面的代码) 到 QuestionVC
例如,当在“数学”部分中轻按“测验1”单元格时, QuestionVC 应显示数学数组的第一部分:
[Quiz(title: "Quiz1", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)]
QuizzesVC 代码:
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) {
}
}
这是 QuestionVC 代码:
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
}
这是 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
}
如果您需要更多说明,请告诉我。
答案 0 :(得分:2)
在didSelectRowAt中,您可以获得单元格的索引,因此:
let itemToPass = mathQuizes[indexPath.row]
比执行您的搜寻更重要:
performSegue(withIdentifier: "yourSegue", sender: itemToPass)
我上面解释过的完整代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let itemToPass = mathQuizes[indexPath.row]
performSegue(withIdentifier: "yourSegue", sender: itemToPass)
}
因此,您现在可以使用
将项目传递到下一个视图控制器。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
}
}
}