使用 UIButton 进行选择并使用 segue 移动特定的模型数据



我是 swift 的新手,也是一般编程的新手。我正在构建一个测验应用程序。我想在 TopicViewController 中选择一个主题,然后移动到一个新的测验视图控制器,该控制器将显示问题和答案选项。我有多个题库,我认为它们是类问题的对象。我可以使用 segue 移动到测验视图主计长,但无法根据所选主题 UI 按钮选择题库。 我已经尝试并花了几天时间试图弄清楚这一点。我在SO中查看过类似的帖子。我之前发布过这个问题,但没有得到任何回复。如果有人能帮忙,我将不胜感激。我不知道还能怎么做...

主题视图控制器:

import UIKit
class TopicsViewController: UIViewController, ReturnToTopicVCDelegate {
func goToTopicVC() {}
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func goToQuestionsVCWhenPressed(_ sender: UIButton) {
performSegue(withIdentifier: "segueToQuestionVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToQuestionVC" {
let quizVC = segue.destination as! QuizViewController
quizVC.delegate = self
}
}
}

测验视图控制器:

import UIKit
import QuartzCore
protocol ReturnToTopicVCDelegate {
func goToTopicVC()
}
class QuizViewController: UIViewController {
var delegate: ReturnToTopicVCDelegate?
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var questionImageView: UIImageView!
@IBOutlet weak var optionAButton: UIButton!
@IBOutlet weak var optionBButton: UIButton!
@IBOutlet weak var optionCButton: UIButton!
@IBOutlet weak var optionDButton: UIButton!
@IBOutlet weak var optionEButton: UIButton!
//outlets for the progress
@IBOutlet weak var questionCounter: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
var allQuestions = QuestionBank()
var selectedAnswer: Int = 0 // answer selected by the subject
var questionNumber: Int = 0
var score: Int = 0
@IBAction func answerPressed(_ sender: UIButton) {
if sender.tag == selectedAnswer {
print("correct answer")
sender.backgroundColor = .green
score += 1
} else {
print("wrong")
sender.backgroundColor = .red
print("(allQuestions.list[questionNumber].correctAnswer)")
//the following two lines change the right answer button to green using the tag value of the button
let correctAnswerButton = view.viewWithTag(selectedAnswer) as? UIButton
correctAnswerButton?.backgroundColor = UIColor.green
}
}
@IBAction func GoToNextQuestion(_ sender: UIButton) {
questionNumber += 1
nextQuestion()
}
func nextQuestion() {
if questionNumber <= allQuestions.list.count - 1 {
questionLabel.text = allQuestions.list[questionNumber].question
questionImageView.image = UIImage(named: (allQuestions.list[questionNumber].questionImage))
optionAButton.setTitle(allQuestions.list[questionNumber].optionA, for: .normal)
optionBButton.setTitle(allQuestions.list[questionNumber].optionB, for: .normal)
optionCButton.setTitle(allQuestions.list[questionNumber].optionC, for: .normal)
optionDButton.setTitle(allQuestions.list[questionNumber].optionD, for: .normal)
optionEButton.setTitle(allQuestions.list[questionNumber].optionE, for: .normal)
selectedAnswer = allQuestions.list[questionNumber].correctAnswer
updateUI()
} else {
let alert = UIAlertController(title: "Great!", message: "Do you want to start over?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default) { (UIAlertAction) in
self.restartQuiz()
}
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}
}
func updateUI() {
scoreLabel.text = "score: (score)"
questionCounter.text = "(questionNumber + 1)/(allQuestions.list.count)"
}
func restartQuiz() {
score = 0
questionNumber = 0
nextQuestion()
}
@IBAction func goBackToTopicsVC(_ sender: Any) {
delegate?.goToTopicVC()
dismiss(animated: true, completion: nil)
}
}

问题采用这种格式

import Foundation
class QuestionBank {
var list = [Question]()
init() {
let skyColorQuestion = Question(questionText: “What is the color of sky?", image: "sky", choiceA: "blue", choiceB: "black", choiceC: "yellow", choiceD: "pink", choiceE: "None of the above", answer: 1)
let whatQuestion = Question(questionText: “what…?”, image: "image", choiceA: "x", choiceB: "y", choiceC: "z", choiceD: "m", choiceE: "None of the above", answer: 3)
list.append(skyColorQuestion)
list.append(whatQuestion)
}
}

导航窗格

脚本

以这种方式添加数据不正确。我的建议你可以使用 Realm、sqLite、CoreData 或 FireBase。

最新更新