在视图控制器之间传递 类?
passing classes between view controllers?
我正在构建一个测验应用程序,我试图通过让一个视图控制器充当问题视图控制器来减少视图控制器的数量。这是用户进行测验的视图控制器。
我有多个问题库,其中包含特定类别的问题。这些问题库是 .swift 文件,我认为它们被 class 化为 classes,它们看起来像这样:
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 示例代码(但有实际问题 lol)
import UIKit
import Foundation
class QuestionsViewController: UIViewController {
var allQuestions = GeographyQuestionBank()
...
我了解如何使用 prepare(for segue:...) 函数在视图控制器之间传递内容。但我不确定如何将 class 传递给 allQuestions 变量。那可能吗?
我希望这是有道理的,如果没有,请发送消息,我会尽力解释得更好。但我只想根据在前一个视图控制器上选择的类别,将问题库 classes 传递给 QuestionsViewController。
所以在 Swift 中,您不会将 classes 本身传递给视图控制器,因为您可以在任何地方(通常)实例化它们,也许您指的是传递 class 的实例 到不同的视图控制器?
如果您的问题是静态的,即您只是按类别列出它们,而不是在它们被放入后进行更改,那么您真的不需要 class 个问题。您可以将它们放在 struct
或 enum
中,以便在您需要的任何地方都可以公开访问它们。
无需将问题实例或所有问题传递给不同的视图控制器。如果您将它们全部收集在 allQuestions
变量中,则可以根据您的实现迭代或删除元素
如果像下面这样将问题数组传递给第二个视图控制器,就会变得非常简单。
第一视图控制器
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
}
}
第二个视图控制器
class SecondViewController: UIViewController {
var questionList:[Questions] = []
override func viewDidLoad() {
super.viewDidLoad()
println(questionList) //You get selected list here
}
}
我正在构建一个测验应用程序,我试图通过让一个视图控制器充当问题视图控制器来减少视图控制器的数量。这是用户进行测验的视图控制器。
我有多个问题库,其中包含特定类别的问题。这些问题库是 .swift 文件,我认为它们被 class 化为 classes,它们看起来像这样:
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 示例代码(但有实际问题 lol)
import UIKit
import Foundation
class QuestionsViewController: UIViewController {
var allQuestions = GeographyQuestionBank()
...
我了解如何使用 prepare(for segue:...) 函数在视图控制器之间传递内容。但我不确定如何将 class 传递给 allQuestions 变量。那可能吗?
我希望这是有道理的,如果没有,请发送消息,我会尽力解释得更好。但我只想根据在前一个视图控制器上选择的类别,将问题库 classes 传递给 QuestionsViewController。
所以在 Swift 中,您不会将 classes 本身传递给视图控制器,因为您可以在任何地方(通常)实例化它们,也许您指的是传递 class 的实例 到不同的视图控制器?
如果您的问题是静态的,即您只是按类别列出它们,而不是在它们被放入后进行更改,那么您真的不需要 class 个问题。您可以将它们放在 struct
或 enum
中,以便在您需要的任何地方都可以公开访问它们。
无需将问题实例或所有问题传递给不同的视图控制器。如果您将它们全部收集在 allQuestions
变量中,则可以根据您的实现迭代或删除元素
如果像下面这样将问题数组传递给第二个视图控制器,就会变得非常简单。
第一视图控制器
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
}
}
第二个视图控制器
class SecondViewController: UIViewController {
var questionList:[Questions] = []
override func viewDidLoad() {
super.viewDidLoad()
println(questionList) //You get selected list here
}
}