Hey guys, so for some reason, I've made a UILabel and it presents questions, so instead of it saying the real question that's meant to come, it says the future question. For example: The UILabel is meant to say 5 + 5 = 55 and if it's true or false, if I say its false the console says I got it wrong and says that the question I just answered is 12 + 0, but then the UILabel's current question the UILabel says the next question is 12 + 0. What's happening and can you help me?
var timer = Timer()
var countdown : Int = 60
var allQuestions = QB1().listY1.count
var questionNumber : Int = 1
var UserPickedAnswer : Bool = false
var score : Int = 1
var consoleNumTracker : Int = 1
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var trueButton: UIButton!
@IBOutlet weak var falseButton: UIButton!
@IBOutlet weak var IGScoreLabel: UILabel!
@IBOutlet weak var startButtonOutlet: UIButton!
@IBOutlet weak var countdownLabel: UILabel!
@IBOutlet weak var backButton: UIBarButtonItem!
@IBAction func startButton(_ sender: Any) {
startButtonOutlet.isHidden = true
print(QB1().listY1.count)
questionLabel.text = String(QB1().listY1[questionNumber].questionText)
countdown = 60
TimerStart()
}
func TimerStart() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(TimerCD), userInfo: nil, repeats: true)
}
@objc func TimerCD() {
countdown = countdown - 1
countdownLabel.text = String(countdown)
if countdown == 1 {
countdown = countdown - 1
timer.invalidate()
timesUp()
}
}
func timesUp() {
let TimeUp = UIAlertController(title: "Time's Up!", message: "Congrats, you finished! Your score was \(score)!", preferredStyle: .alert)
let TUAction = UIAlertAction(title: "Back To Home", style: .default, handler: { UIAlertAction in
self.EnableSeeResult()
})
TimeUp.addAction(TUAction)
present(TimeUp, animated: false, completion: nil)
}
/*––––––
Enable Results Button
–––––—*/
func EnableSeeResult() {
IGScoreLabel.text = "Score: " + String(score) + "/41"
falseButton.isEnabled = false
trueButton.isEnabled = false
timer.invalidate()
}
/*––––––
Enable Results Button
–––––—*/
func updateUI1() {
IGScoreLabel.text = "Score: " + String(score)
}
@IBAction func TrueOrFalseClickedY2(_ sender: AnyObject!) {
if sender.tag == 1 {
UserPickedAnswer = true
}
else if sender.tag == 2 {
UserPickedAnswer = false
}
NQ2()
questionLabel.text = QB2().listY2[questionNumber].questionText
updateUI2()
}
Problem comes from the order of statements in NQx() func.
You change the question before checking the current answer.
Replace
func NQ1() {
questionNumber = Int(arc4random_uniform(UInt32(Int(QB1().listY1.count))))
questionLabel.text = QB1().listY1[questionNumber].questionText
checkAnswer1()
}By
func NQ1() {
checkAnswer1()
questionNumber = Int(arc4random_uniform(UInt32(Int(QB1().listY1.count))))
questionLabel.text = QB1().listY1[questionNumber].questionText
}In addition, note that func names should start by lowercase ; and names should be more explicit.
You could also replace Arc4random by the newest Int.random
questionNumber = Int.random(in: 0..<QB1().listY1.count)
And you should also test whether the question was already asked.