启用和禁用按钮 Xcode 8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39275931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:11:42  来源:igfitidea点击:

enable and disable a button Xcode 8

iosswiftxcode

提问by Colton Carnevale

I am trying to create a way to disable a Button. This is an app I made that is an egg timer, and I run into one bug; when I press the play button multiple times the timer speeds up and I can't get it to stop. I want to create a disable feature, but everything I've seen on the forum says to use. enable = true. When I use this Xcode says that it isn't valid. What is the proper code for enabling and disabling a button in Xcode 8?

我正在尝试创建一种禁用Button. 这是我制作的一个鸡蛋计时器应用程序,我遇到了一个错误;当我多次按下播放按钮时,计时器会加速并且无法停止。我想创建一个禁用功能,但我在论坛上看到的所有内容都说可以使用。enable = true. 当我使用这个 Xcode 时说它无效。在 Xcode 8 中启用和禁用按钮的正确代码是什么?

import UIKit

class ViewController: UIViewController {

    var timer = Timer()
    var myCount = 210
    var button = 0

    func processTimer() {

        //what happens every second

        counter()

    }

    func startTimer() {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.processTimer), userInfo: nil, repeats: true)
        button = 1
    }

    func pauseTimer() {
        timer.invalidate()
        button = 0
    }

    func resetTimer(){
        timer.invalidate()
        myCount = 210
        countdown.text = "\(myCount)"
        button = 0
    }

    func counter() {
        myCount -= 1
        if myCount > 0 {
            countdown.text = "\(myCount)"
        } else {
            countdown.text = "0"
            timer.invalidate()
        }
    }

    func add(){
        myCount += 10
        if myCount > 0 {
            countdown.text = "\(myCount)"
        } else {
            countdown.text = "0"
        }
    }

    func sub(){
        myCount -= 10
        if myCount > 0 {
            countdown.text = "\(myCount)"
        } else {
            countdown.text = "0"
        }
    }






    // timer countdown
    @IBOutlet var countdown: UILabel!

    // pause button
    @IBAction func pauseButton(_ sender: AnyObject) {
        pauseTimer()
        print("Timer Paused")
    }


    //play button
    @IBAction func playButton(_ sender: AnyObject) {
        startTimer()
        print("Timer started")
    }


    // -10 seconds
    @IBAction func minusTen(_ sender: AnyObject) {
        sub()



    }
    // reset timer to 290
    @IBAction func resetButton(_ sender: AnyObject) {
        resetTimer()
        print("Timer Reset")
    }






    // +10 seconds
    @IBAction func addTen(_ sender: AnyObject) {
        add()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        countdown.text = "\(myCount)"

        if button == 0{
            playButton.enabled = true
        }




    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

回答by Nirav D

There is two ways to enabled/disablethe Button.

有两种方法enabled/disableButton

playButton.isEnabled = false // This will give the (style) effect of disable to button

or

或者

playButton.userInteractionEnabled = false // This will not give the (style) effect of disable
                                         to button simply stop the user interaction

回答by Russell

First thing you need to do is add an outlet for your button. You have created the function, but you need a separate outlet if you want to make changes - something like this

您需要做的第一件事是为按钮添加一个插座。您已经创建了该功能,但是如果您想进行更改,则需要一个单独的插座 - 像这样

    @IBOutlet weak var cmdPlayButton: UIButton!

then, if you want to prevent multiple presses on the button while the timer is running, you need to disable the button as soon as it is pressed, and only re-enable it when the timer finishes

然后,如果您想在计时器运行时防止多次按下按钮,则需要在按下按钮后立即禁用该按钮,并且仅在计时器完成时重新启用它

//play button
@IBAction func playButton(_ sender: AnyObject) {
    cmdPlayButton.enabled = false
    startTimer()
    print("Timer started")
}

just remember to enable the button when the timer finishes or is reset, and you will need to handle the pause functionality - you might change the text on the play button to read 'restart' for instance.

只需记住在计时器完成或重置时启用按钮,您将需要处理暂停功能 - 例如,您可以将播放按钮上的文本更改为“重新启动”。