每次我离开这个ViewController然后回来的时候,测试都会回到问题1。我希望它能从我离开的地方恢复。
我认为它之所以一直重置为零是因为我的初始“questionNumber”设置为0,但我不确定如何将其值正确设置为Firebase中的值。一个接一个的错误。我尝试过很多不同的方法,但似乎没有一种能从我离开的地方恢复工作。
谢谢你的帮助!请帮帮我!
class CryptoViewController: UIViewController {
var questionList = CryptoBank()
var score = 0
var pickedQuestion = 0
var uid = FIRAuth.auth()?.currentUser?.uid
var questionNumber = 0
@IBOutlet weak var questionViewer: UILabel!
@IBOutlet weak var choiceOne: UIButton!
@IBOutlet weak var choiceTwo: UIButton!
@IBOutlet weak var choiceThree: UIButton!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var questionNumberView: UILabel!
@IBOutlet weak var progressBarView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
choiceOne.titleLabel?.textAlignment = NSTextAlignment.center
choiceTwo.titleLabel?.textAlignment = NSTextAlignment.center
choiceThree.titleLabel?.textAlignment = NSTextAlignment.center
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
update()
}
@IBAction func buttonPressed(_ sender: AnyObject) {
if sender.tag == 1 {
pickedQuestion = 1}
else if sender.tag == 2 {
pickedQuestion = 2}
else if sender.tag == 3 {
pickedQuestion = 3}
checkAnswer()
questionNumber += 1
nextQuestion()
}
func checkAnswer(){
let correctAnswer = questionList.cryptoBank[questionNumber].answer
if pickedQuestion == correctAnswer {
score += 1
}else{
print("Wrong Answer")
}
}
func updateFirebase(){
let ref = FIRDatabase.database().reference()
guard let uid = FIRAuth.auth()?.currentUser!.uid else{
return}
ref.child("Users").child(uid).child("Cryptoquiz").child("Question Number").setValue(questionNumber)
ref.child("Users").child(uid).child("Cryptoquiz").child("Score").setValue(score)
}
func nextQuestion(){
if questionNumber <= 9 {
update()
} else{
scoreLabel.text = "Score: \(score)"
let alert = UIAlertController(title: "Done!", message: "Would you like to restart?", preferredStyle: .alert)
let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
self.startAgain()
})
alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}}
func update(){
let nextQuest = questionList.cryptoBank[questionNumber]
questionViewer.text = nextQuest.question
choiceOne.setTitle(nextQuest.choice1, for: .normal)
choiceTwo.setTitle(nextQuest.choice2, for: .normal)
choiceThree.setTitle(nextQuest.choice3, for: .normal)
scoreLabel.text = "Score: \(score)"
questionNumberView.text = "Question: \(questionNumber + 1)"
progressBarView.frame.size.width = (view.frame.size.width/9) * CGFloat(questionNumber + 1)
updateFirebase()
}
}
最佳答案
您可以将进度保存在UserDefaults
中,如下所示:
UserDefaults.standard.set(questionNumber, forKey: "questionNumber")
然后每次打开视图时,在
viewDidDisappear()
或viewDidLoad()
中,您都会更新您的问题编号,如下所示:let questionNumberSaved = UserDefaults.standard.integer(forKey: “questionNumber”) ?? 0
questionNumber = questionNumberSaved
然后在执行此操作后,调用
viewDidAppear()
方法关于swift - 如何将我的变量正确设置为Firebase上的值,而不用将其重置进度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53184310/