【发布时间】:2021-03-13 01:33:13
【问题描述】:
我正在构建一个测验应用程序,在测验结束时,单击答案后,我希望屏幕切换到显示用户分数的 ResultViewController。我将我的 MCViewController 中的 segue 链接到新的 ResultViewController 屏幕。但是在完成测验后,屏幕有点暗,没有错误。
MCViewController:
import UIKit
class MCViewController: UIViewController {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var progressBar: UIProgressView!
@IBOutlet weak var aButton: UIButton!
@IBOutlet weak var bButton: UIButton!
@IBOutlet weak var cButton: UIButton!
@IBOutlet weak var dButton: UIButton!
let quiz =
[
Questions(q: "When did English settlement begin in Canada?", a: "1510", b: "1497", c: "1604", d: "1720", answer: "1604"),
Questions(q: "Who passed the Quebec Act of 1774?", a: "Canadian Parliament", b: "British Parliament", c: "Quebec Parliament", d: "The French majority", answer: "British Parliament"),
Questions(q: "Whose portrait is on the Canadian 10 dollar bill?", a: "Sir George Cartier", b: "Sir Etienne Tache", c: "Sir John A. Macdonald", d: "Sir Louis La Fontaine", answer: "Sir John A. Macdonald"),
Questions(q: "What are the responsibilities of the federal government?", a: "Matters of national and international concern.", b: "Matters of national concern.", c: "Matters of international concern.", d: "Matters of provincial concern.", answer: " Matters of national and international concern."),
Questions(q: "What is 'Habeas corpus'?", a: "The right to challenge unlawful detention by the state.", b: "The right to live and work anywhere in Canada.", c: "The right to speak freely.", d: " The right for peaceful assembly.", answer: "The right to challenge unlawful detention by the state.")
]
var questionNumber = 0
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
@IBAction func answerPressed(_ sender: UIButton) {
let userAnswer = sender.currentTitle
let actualAnswer = quiz[questionNumber].answer
if (userAnswer == actualAnswer) {
sender.backgroundColor = UIColor.green
} else {
sender.backgroundColor = UIColor.red //END OF ARRAY, SHOULD TRANSITION TO RESULTVIEWCONTROLLER
}
if (questionNumber + 1 < quiz.count){
questionNumber += 1
} else {
let resultVC = ResultViewController()
self.present(resultVC, animated: true, completion: nil)
}
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(updateUI), userInfo: nil, repeats: false)
}
@objc func updateUI() {
questionLabel.text = quiz[questionNumber].q
aButton.setTitle(quiz[questionNumber].a, for: .normal)
bButton.setTitle(quiz[questionNumber].b, for: .normal)
cButton.setTitle(quiz[questionNumber].c, for: .normal)
dButton.setTitle(quiz[questionNumber].d, for: .normal)
aButton.titleLabel?.adjustsFontSizeToFitWidth = true;
bButton.titleLabel?.adjustsFontSizeToFitWidth = true;
cButton.titleLabel?.adjustsFontSizeToFitWidth = true;
dButton.titleLabel?.adjustsFontSizeToFitWidth = true;
aButton.backgroundColor = UIColor.clear
bButton.backgroundColor = UIColor.clear
cButton.backgroundColor = UIColor.clear
dButton.backgroundColor = UIColor.clear
progressBar.progress = Float(questionNumber + 1) / Float(quiz.count)
}
}
结果视图控制器:
import UIKit
class ResultViewController : UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
}
}
【问题讨论】:
标签: swift xcode storyboard segue