简体   繁体   English

Swift - 为什么我在函数中的函数没有被调用?

[英]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.问题存储在单独文件中的数组 0 - 12 中。 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.在每个问题之后运行的函数中都有一个 if/else 语句 - 如果问题编号变量 <= 12,则提出下一个问题并且问题编号变量增加 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.目标是当用户遇到问题 #12 时,他们会看到一个 UIAlertView 来祝贺他们,并提供一个使用“重新启动”按钮重新启动的机会。

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. “重新启动”按钮运行一个函数,该函数将问题编号变量设置回 0,并且应该以询问第一个问题的 if/else 语句开始该函数。 (Changes the UI text) (更改 UI 文本)

 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.但是,按“重新启动”时,UI 文本不会从最后一个问题更改为第一个问题,并且启动更改的唯一方法是按“真”或“假”按钮。

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.由于“真”或“假”按钮没有启动重启警报(问题数不超过 12),我可以假设函数运行正确并将我的变量设置为 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. Tl; dr:按重新启动按钮运行一个成功设置新变量的函数,但不运行指定的附加函数。 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!非常感谢您的帮助 - 仍然是 Swift 的新手并且渴望学习!

The reason is the button is named restart.原因是按钮被命名为重新启动。 There are other functions in swift that are also restart() . swift 中还有其他函数也是restart() Just change it and it should work.只需更改它,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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