在IBAction闭包中禁用UIButton-Swift 4

时间:2019-02-05 05:21:36

标签: uibutton swift4 ibaction

当用户在玩我的应用程序时,某些按钮是我不希望他们意外按下的按钮。因此,当应用程序位于gameMode = 1中时,我想显示为灰色并禁用它们。以下代码在应有的情况下禁用了该按钮,但在我需要时(在gameMode = 0中)则不再启用它。而且按钮不会变灰。

@IBAction func menuButton(_ sender: UIButton) {  

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let myVC = storyboard.instantiateViewController(withIdentifier: "viewController")

    if gameMode == 0 {

        sender.isEnabled = true
        self.present(myVC, animated: false, completion: nil)

    } else if gameMode == 1 {
        sender.isEnabled = false

    }
}

1 个答案:

答案 0 :(得分:0)

@IBAction func menuButton(_ sender: UIButton) {  

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let myVC = storyboard.instantiateViewController(withIdentifier: "viewController")

    if gameMode == 0 {

        sender.isUserInteractionEnabled = true
        self.present(myVC, animated: false, completion: nil)

    } else if gameMode == 1 {
        sender.isUserInteractionEnabled = false

    }
}