【发布时间】:2026-01-11 06:05:02
【问题描述】:
我正在创建一个包含多个类别的测验应用程序。如果用户想要选择一个类别,他们将“打开”该类别的开关。如果开关打开,则 Bool 值将设置为 true,如果开关关闭,则设置为 false。然后这个 Bool 值将被发送到下一个视图控制器,如果它有一个真值,就会运行一个函数,将所选类别的问题的数组添加到一个包含所选类别数组的数组中。
目前,这是我的应用程序的功能: 我有一个名为 Question 的快速文件,它为问题的基本结构创建了一个类。然后我有另一个名为 QuestionBank 的文件,其中包含一个类,在该类中是我使用 Question.swift 文件中的类创建的问题数组。从那里,在我的游戏的视图控制器中,我调用包含问题数组的类,然后我的代码按照数组中调用它们的顺序显示每个问题及其所有选项。我将如何做到这一点,以便用户可以根据用户输入访问数组中的特定问题或特定问题组?我已经尝试创建多个问题数组并尝试调用这些问题并将其组合起来,以便可以在这两个类别上测试用户,但由于某种原因我无法做到。
第一个视图控制器:
import UIKit
class ViewControllerTrivia: UIViewController {
var historySelection = Bool()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let gameController = segue.destination as! ViewControllerGame
gameController.isHistorySelected = historySelection
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
@IBAction func swipeToHome(_ sender: Any) {
performSegue(withIdentifier: "triviaToHome", sender: ViewControllerHome.self)}
@IBAction func startGame(_ sender: Any) {
performSegue(withIdentifier: "triviaToGame", sender: ViewControllerGame.self)
}
@IBAction func historySelectionSwitch(_ sender: UISwitch) {
if (sender.isOn == true) {
var historySelection = true
print("on")
} else {
var historySelection = false
print("off")
}
}
}
Second View Controller:
import UIKit
class ViewControllerGame: UIViewController {
var isHistorySelected = Bool()
@IBOutlet weak var questionCounter: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var progressView: UIView!
@IBOutlet weak var questionLabel: UILabel!
//Outlet for Buttons
@IBOutlet weak var optionA: UIButton!
@IBOutlet weak var optionB: UIButton!
@IBOutlet weak var optionC: UIButton!
@IBOutlet weak var optionD: UIButton!
var allQuestions = [(Question(questionText: "History: What does FBLA stand for?", choiceA: "A. Formidable Business Learners of America", choiceB: "B. Future Business Learners of America", choiceC: "C.Future Business Leaders of America", choiceD: "D.Fleeting Business Learners Of America", answer: 3)),]
var historyQuestions = [(Question(questionText: "History: Who was the founder of the FBLA program?", choiceA: "A. Edward D. Miller", choiceB: "B. Conrad N. Hilton", choiceC: "C. Hamden L. Forkner", choiceD: "Elena Daly", answer: 3)) ]
var questionNumber: Int = 0
var score: Int = 0
var selectedAnswer: Int = 0
func questionSelection() {
if isHistorySelected == true{
allQuestions.append(contentsOf: historyQuestions)
}
}
//Making only the top corners of the progress view visible
func roundProgressCorners() {
self.progressView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
}
override func viewDidLoad() {
super.viewDidLoad()
roundProgressCorners()
questionSelection()
updateQuestion()
updateUI()
// Do any additional setup after loading the view.
}
@IBAction func answerPressed(_ sender: UIButton) {
if sender.tag == selectedAnswer{
print("correct")
score += 1
}else {
print("wrong")
}
questionNumber += 1
updateQuestion()
}
func updateQuestion(){
if questionNumber <= allQuestions.count - 1{
questionLabel.text = allQuestions[questionNumber].question
optionA.setTitle(allQuestions[questionNumber].optionA, for: UIControl.State.normal)
optionB.setTitle(allQuestions[questionNumber].optionB, for: UIControl.State.normal)
optionC.setTitle(allQuestions[questionNumber].optionC, for: UIControl.State.normal)
optionD.setTitle(allQuestions[questionNumber].optionD, for: UIControl.State.normal)
selectedAnswer = allQuestions[questionNumber].correctAnswer
updateUI()
}else {
restartQuiz()
performSegue(withIdentifier: "gameToEndScreen", sender: ViewControllerEndScreen.self)
}
}
func updateUI(){
scoreLabel.text = "Score: \(score)"
questionCounter.text = "\(questionNumber + 1)/\(allQuestions.count)"
progressView.frame.size.width = (view.frame.size.width / CGFloat(allQuestions.count)) * CGFloat(questionNumber + 1)
}
func restartQuiz(){
score = 0
questionNumber = 0
updateQuestion()
}
}
对于最终结果,我希望用户能够选择他们想要进行测验的类别,然后应用程序将组合所选类别。
【问题讨论】:
-
你能更新问题类吗,你想根据类别过滤问题..?
标签: swift