arrays - 我需要使用 Array 将数据从 UITableViewController 传递到 UIViewController
问题描述
这是我第一次构建自己的应用程序,我需要帮助,该应用程序是关于测验的。
该应用程序有很多材料,每种材料都有很多测验,这是QuizzesVC
我想将QuizzesArray中的数据(代码在下面)传递给QuestionVC
例如,当在数学部分点击 Quiz 1 单元格时,QuestionVC应该显示数学数组的第一部分:
[Quiz(title: "Quiz1", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)]
测验VC代码:
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
}
如果您需要更多解释,请告诉我。
解决方案
在 didSelectRowAt 你可以得到单元格的索引,所以:
let itemToPass = mathQuizes[indexPath.row]
而不是执行你的segue:
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
}
}
}
推荐阅读
- python - 在 Pythin 中发送 ajax 标头
- java - 您如何获得比“错误:程序类型已存在:
“? - sql - JOIN SQL 查询失败
- css - 覆盖下的可点击链接,隐藏在悬停选择器上
- symfony - Symfony 4,如何从服务类中正确获取根路径目录(或 /src 路径)?(错误:'在 null 上调用成员函数 get()')
- git - 检索给定 Git 存储库的 URL 的最新修订 ID?
- google-apps-script - Google Sheets 脚本根据单元格的值对单元格进行条件格式设置
- r - 如何将代码应用于多个 Excel 工作簿/工作表
- vue.js - 带有url查询的vue.js路由器活动链接
- excel - Excel VBA:根据偏移值将动态范围复制到最后一行