gpt4 book ai didi

ios - 在 View Controller 之间传递类?

转载 作者:行者123 更新时间:2023-11-30 11:50:10 24 4
gpt4 key购买 nike

我正在构建一个测验应用程序,并且我试图通过让一个 View Controller 充当问题 View Controller 来减少 View Controller 的数量。这是用户进行测验的 View Controller 。

我有多个问题库,其中包含特定于某个类别的问题。这些题库是 .swift 文件,我认为它们被分类为类,它们看起来像这样:

import Foundation

class QuestionBank {

var list = [Questions]()

init() {

let item = Questions(text: "what does blah blah blah mean?", correctAnswer: "blah blah", textA: "blah blah blah", textB: "blah", textC: "blah bla", textD: "blee blah" )

list.append(item)

list.append(Questions(text: "this is a question?", correctAnswer: "correct answer", textA: "examplea", textB: "exampleb", textC: "examplec", textD: "exampled"))

list.append(Questions(text: ".......

list.append(Questions(text: ".......

list.append(Questions(text: ".......

}


}

下面只是 QuestionViewController 的第一行,但它显示 var allQuestions 保存着 GeographyQuestionBank。 GeographyQuestionBank 看起来像上面的 QuestionBank 示例代码(但有实际问题哈哈)

import UIKit

import Foundation

class QuestionsViewController: UIViewController {

var allQuestions = GeographyQuestionBank()

...

我了解如何使用prepare(for segue:...) 函数在 View Controller 之间传递内容。但我不确定如何将类传递给 allQuestions 变量。那可能吗?

我希望这是有道理的,如果没有,请发送消息,我会尽力解释得更好。但我只是希望能够根据前一个 View Controller 上选择的类别将问题库类传递到 QuestionsViewController 。

最佳答案

如果将问题数组传递给第二个 View Controller ,事情就会变得非常简单,如下所示。

第一个 View Controller

import UIKit

class FirstViewController: UIViewController {

var selectedCategoryQuestions :[Questions] = []
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func onClickBtnHistoryQuestions(_ sender: Any)
{
//Fill your array with History Question
self.performSegue(withIdentifier: "yourSecondVC", sender: nil)
}
@IBAction func onClickBtnGeoQuestions(_ sender: Any) {
//Fill your array with Geo Question
self.performSegue(withIdentifier: "yourSecondVC", sender: nil)
}

if (segue.identifier == "yourSecondVC")
{
let destVC:yourSecondVC = segue.destination as! yourSecondVC
destVC.questionList = selectedCategoryQuestions

}
}

第二个 View Controller

 class SecondViewController: UIViewController {

var questionList:[Questions] = []
override func viewDidLoad() {
super.viewDidLoad()


println(questionList) //You get selected list here
}

}

关于ios - 在 View Controller 之间传递类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48414591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com