如果用户已经点击了正确答案,如何停止递增数字?

时间:2015-05-18 02:08:35

标签: ios function swift

我正在制作一个测验应用,我想要它,以便当用户点击正确的答案时,他们的分数会增加1。但是,如果他们继续点击相同的答案而不继续下一个问题,他们很容易增加分数。

 @IBAction func Button1Action(sender: AnyObject) {
        unhideLabel()
        if(correctAnswer == "1") {
            correctOrWrong.text = "Correct"
            scoreNumber++
            score.text = "Score: \(scoreNumber)"
        }
        else {
            correctOrWrong.text = "Incorrect"
            hideNextQuestion()
        }
    }

    @IBAction func Button2Action(sender: AnyObject) {
        unhideLabel()
        if (correctAnswer == "2") {
            correctOrWrong.text = "Correct"
            scoreNumber++
            score.text = "Score: \(scoreNumber)"
    }
        else {
            correctOrWrong.text = "Incorrect"
            hideNextQuestion()

        }
    }

    @IBAction func Button3Action(sender: AnyObject) {
        unhideLabel()
        if (correctAnswer == "3") {
            correctOrWrong.text = "Correct"
            scoreNumber++
            score.text = "Score: \(scoreNumber)"
    }
        else {
            correctOrWrong.text = "Incorrect"
            hideNextQuestion()
        }
    }

    @IBAction func Button4Action(sender: AnyObject) {
        unhideLabel()
        if (correctAnswer == "4") {
            correctOrWrong.text = "Correct"
            scoreNumber++
            score.text = "Score: \(scoreNumber)"
    }
        else {
            correctOrWrong.text = "Incorrect"
            hideNextQuestion()
        }
    }
}

如果我想在用户已经点击正确答案的情况下停止分数增加,我需要添加/更改什么?

4 个答案:

答案 0 :(得分:0)

您可以创建一个布尔变量来跟踪问题是否已被回答。从那里开始,只需在增加分数之前检查,只有在布尔值尚未设置为true时才增加分数,并且在增加分数时也将布尔值设置为true。这样,分数只会增加一次。

答案 1 :(得分:0)

只需保留BOOL属性即可确定用户是否选择了正确答案。

每次检查时,如果之前已选择过。不要添加分数。

    var hasSelected = false;

@IBAction func Button1Action(sender: AnyObject) {

    if(hasSelected){
        //Tell the user or do something you like
        return;
    }
    if(correctAnswer == "1") {
        correctOrWrong.text = "Correct"

        scoreNumber++

        score.text = "Score: \(scoreNumber)"
        //If user select the right answer
        hasSelected = true;

    }
    else {
        correctOrWrong.text = "Incorrect"
        hideNextQuestion()

    }

}

我认为更好的设计是

当用户选择正确的时候,自动进入下一个问题

答案 2 :(得分:0)

我建议你编写一个函数,而不是用相同的实现编写四个不同的函数。您可以使用tag UIButton属性来确定运行时单击的按钮。 让我们说你的button1有标签值1,button2有标签值2 ....然后你可以为所有四个按钮点击写一个通用动作..

为了让您的问题停止增加分数,我建议您在用户第一次点击正确答案时禁用该按钮,并在下一个问题加载时启用所有按钮。

 @IBAction func ButtonAction(sender: AnyObject) {
        unhideLabel()
        if ((sender as UIButton).tag == correctAnswer?.toInt()) {
           (sender as UIButton).enabled = false;
            correctOrWrong.text = "Correct"
            scoreNumber++

            score.text = "Score: \(scoreNumber)"
    }
        else {
            correctOrWrong.text = "Incorrect"
            hideNextQuestion()

        }
    }

答案 3 :(得分:0)

我认为更好的方法是使用UIButton selected属性,这是一个bool。代码就是这样的。而且我认为你在所有4个案例中都做了同样的事情。那么为什么你不能使用一些传递答案参数的常用方法。这将简化代码。 对不起,如果我错了。希望这能帮到你..

 @IBAction func Button1Action(sender: UIButton) {
            unhideLabel()
            if(correctAnswer == "1" && sender.selected != true) {
                correctOrWrong.text = "Correct"
                scoreNumber++
                score.text = "Score: \(scoreNumber)"
                sender.selected=true
            }
            else {
                correctOrWrong.text = "Incorrect"
                hideNextQuestion()
            }
        }

        @IBAction func Button2Action(sender: UIButton) {
            unhideLabel()
            if (correctAnswer == "2" && sender.selected != true) {
                correctOrWrong.text = "Correct"
                scoreNumber++
                score.text = "Score: \(scoreNumber)"
                sender.selected=true
        }
            else {
                correctOrWrong.text = "Incorrect"
                hideNextQuestion()
            }
        }

        @IBAction func Button3Action(sender: UIButton) {
            unhideLabel()
            if (correctAnswer == "3" && sender.selected != true) {
                correctOrWrong.text = "Correct"
                scoreNumber++
                score.text = "Score: \(scoreNumber)"
                sender.selected=true
        }
            else {
                correctOrWrong.text = "Incorrect"
                hideNextQuestion()
            }
        }

        @IBAction func Button4Action(sender: UIButton) {
            unhideLabel()
            if (correctAnswer == "4" && sender.selected != true) {
                correctOrWrong.text = "Correct"
                scoreNumber++
                score.text = "Score: \(scoreNumber)"
                sender.selected=true
        }
            else {
                correctOrWrong.text = "Incorrect"
                hideNextQuestion()
            }
        }
    }