【发布时间】:2018-07-03 00:47:02
【问题描述】:
我正在构建一个测验应用程序,并试图通过让一个视图控制器充当问题视图控制器来减少视图控制器的数量。这是用户进行测验的视图控制器。
我有多个题库,其中包含特定类别的问题。这些题库是 .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:... ) 函数在视图控制器之间传递内容。但我不确定如何将类传递给 allQuestions 变量。那可能吗?
我希望这是有道理的,如果没有,请发送消息,我会尽力解释得更好。但我只想能够根据在前一个视图控制器上选择的类别将问题库类传递给 QuestionsViewController。
【问题讨论】:
-
需要更多解释实际上你必须传递任何特定问题或问题列表,因为上面的代码中没有关于任何类别的规范
-
@jaydeepvyas 感谢您的回复。在第一个示例中,“class QuestionBank”只是一个示例。每个问题库都包含一个针对特定类别的问题列表。所以说在前一个视图控制器上有 4 个按钮可供选择。 “地理”、“历史”、“动物”和“数学”。这些类别中的每一个都有自己的题库,即 HistoryQuestionBank.swift、AnimalsQuestionBank.swift 等。所以我想根据选择的类别/按钮在 QuestionsViewController 上传递 var allQuestions 题库
标签: ios swift xcode uiviewcontroller segue