【问题标题】:How can i disable a function, in XCode 8 swift如何在 XCode 8 swift 中禁用功能
【发布时间】:2017-08-13 06:48:49
【问题描述】:

所以我制作了一个测验应用程序,当用户完成 10 个问题时,它会隐藏按钮并显示一个按钮以转到下一个问题。我最近添加了一个倒数计时器,它可以工作,但是当他们完成测试时,计时器会继续,我想在他们完成测验时找到一种方法来禁用它。因此,当他们已经完成时,他们不会被带到重试页面。希望有道理!

更新定时器代码

internal func updateTimer()
{

    counter = counter - 1
    if(counter > 0)
    {
        ibCounter.text = String(counter)
    }else{   
        self.performSegue(withIdentifier: "Segue13", sender: nil)
    }
}

定时器代码。

timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(cricketTest1View.updateTimer), userInfo: nil, repeats: true)

这是我在用户完成测验时输入代码的部分

func PickQuestion(){

        if Questions.count > 0{
            QNumber = 0
            QuestionLabel.text = Questions[QNumber].Question

            AnswerNumber = Questions[QNumber].Answer

            for i in 0..<Buttons.count{

                Buttons[i].setTitle(Questions[QNumber].Answers[i], for: UIControlState())

            }

            Questions.remove(at: QNumber)
        }
        else{

            QuestionLabel.text = "YOU HAVE COMPLETED THE QUIZ"
            Button1.isHidden = true
            Button2.isHidden = true
            Button3.isHidden = true
            Button4.isHidden = true
            NextQuiz.isHidden = false
            Start.isHidden = true
            ibCounter.isHidden = true
        }
}

【问题讨论】:

    标签: ios swift function disabled-input


    【解决方案1】:

    测验完成后使用timer.invalidate()

    它应该根据你的代码放在你的 else 部分。

    【讨论】:

    • 没错,直到现在我才意识到它的存在!
    • @Seth 没问题,如果你愿意,也可以投票。你还没有投票。
    【解决方案2】:

    在表示测验已完成的这些行的正下方,您要停止计时器:

        QuestionLabel.text = "YOU HAVE COMPLETED THE QUIZ"
        Button1.isHidden = true
        Button2.isHidden = true
        Button3.isHidden = true
        Button4.isHidden = true
        NextQuiz.isHidden = false
        Start.isHidden = true
        ibCounter.isHidden = true
    

    如果你查看 Timer 的docs,你会发现一个名为invalidate的方法:

    停止计时器再次触发并请求将其从运行循环中移除。

    这是您需要调用的确切方法:

    timer.invalidate()
    

    【讨论】:

    • @Seth 如果您认为我的回答回答了您的问题,请考虑通过单击该复选标记接受它!
    • 刚刚做了,直到现在我才意识到那里有一个按钮!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 2016-10-16
    • 1970-01-01
    • 2016-10-23
    • 2015-01-31
    相关资源
    最近更新 更多