简体   繁体   中英

Swift - Why is my function within a function not being called?

I am working on a project for a simple quiz app.

 import UIKit

class ViewController: UIViewController {

    let allQuestions = QuestionBank()
    var pickedAnswer: Bool = false
    var questionNumber: Int = 0

    //Place your instance variables here


    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var scoreLabel: UILabel!
    @IBOutlet var progressBar: UIView!
    @IBOutlet weak var progressLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let firstQuestion = allQuestions.list[0]
        questionLabel.text = firstQuestion.questionText

    }

    @IBAction func answerPressed(_ sender: AnyObject) {
        if sender.tag == 1 {
            pickedAnswer = true
        } else if sender.tag == 2 {
            pickedAnswer = false
        }

        checkAnswer()

        questionNumber = questionNumber + 1

        nextQuestion()

    }


    func updateUI() {

    }


    func nextQuestion() {

        if questionNumber <= 12 {
            questionLabel.text = allQuestions.list[questionNumber].questionText
        }
        else {
            let alert = UIAlertController(title: "Quiz Complete", message: "You have completed the quiz, do you want to start over?", preferredStyle: .alert)

            let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
                self.startOver()
            })

            alert.addAction(restartAction)

            present(alert, animated: true, completion: nil)

        }

    }


    func checkAnswer() {

        let correctAnswer = allQuestions.list[questionNumber].answer

        if correctAnswer == pickedAnswer {
            print("You got it!")
        } else {
            print("Wrong!")
        }

    }


    func startOver() {

        questionNumber = 0
        nextQuestion()

    }



}

The user can answer true or false to a set of questions. The questions are stored in an array, 0 - 12, on a separate file. There is a variable to determine the question number.

   var questionNumber: Int = 0

There is an if/else statement within a function run after every question - if the question number variable <= 12, the next question is asked and the question number variable is increased by 1.

 @IBAction func answerPressed(_ sender: AnyObject) {
        if sender.tag == 1 {
            pickedAnswer = true
        } else if sender.tag == 2 {
            pickedAnswer = false
        }


    checkAnswer()

    questionNumber = questionNumber + 1

    nextQuestion()

}


func updateUI() {

}


func nextQuestion() {

    if questionNumber <= 12 {
        questionLabel.text = allQuestions.list[questionNumber].questionText
    }
    else {
        let alert = UIAlertController(title: "Quiz Complete", message: "You have completed the quiz, do you want to start over?", preferredStyle: .alert)

        let restartAction = UIAlertAction(title: "Restart", style: .default, handler: { (UIAlertAction) in
            self.startOver()
        })

        alert.addAction(restartAction)

        present(alert, animated: true, completion: nil)

    }

Otherwise, they are asked if they want to restart.

The goal is that when the user reaches question #12, they are presented with a UIAlertView that congratulates them and offers a chance to restart with the "Restart" button.

The "Restart" button runs a function which sets the question number variable back to 0 and should begin the function with an if/else statement which asks the first question. (Changes the UI text)

 func startOver() {


    questionNumber = 0
    nextQuestion()

}

However, on pressing "Restart" the UI text does not change from the final question to the first question and the only way to initiate a change is to press on the "true" or "false" button.

This runs a different function and skips the first question to the 2nd.

From the fact that the "true" or "false" button didn't initiate the restart alert (the question count not being above 12), I can assume that the function ran correctly and set my variable to 0.

However, function restarting the app did not run when pressing "Restart".

What error is causing this? What can I change to make my function run on pressing "Restart"?

Tl;dr: Pressing the Restart button runs a function which successfully sets a new variable, but doesn't run the additional function specified. The only way to proceed is to press one of the app's "true" or "false" buttons, messing up the flow of the app.

Thanks so much for the help - Still new to Swift and eager to learn!

The reason is the button is named restart. There are other functions in swift that are also restart() . Just change it and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM