首页 > 解决方案 > 我需要使用 Array 将数据从 UITableViewController 传递到 UIViewController

问题描述

这是我第一次构建自己的应用程序,我需要帮助,该应用程序是关于测验的。

故事板的图像

该应用程序有很多材料,每种材料都有很多测验,这是QuizzesVC

QuizzesVC 的图片

我想将QuizzesArray中的数据(代码在下面)传递给QuestionVC

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
}

如果您需要更多解释,请告诉我。

标签: arraysswiftxcodeuitableview

解决方案


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

推荐阅读