如何在 Swift 中隐藏操作按钮?

How to hide an action button in Swift?

我尝试让按钮在按下后不可见。连接是一个动作而不是出口,因为按下按钮会调用额外的代码。

@IBAction func startGame(_ sender: Any) {
    print("The game starts...")

}

这不起作用,因为按钮是一个动作而不是一个出口:

startGame.isHidden = true 

是否有另一种方法可以使操作按钮不可见,因此无法点击?

只需创建同一个按钮的 IBOutlet,并在点击后将其 isHidden 属性 设置为 true

@IBAction func startGame(_ sender: Any) {
     startGameButton.isHidden = true
}

您可以通过这种方式在按下操作时隐藏按钮

 @IBAction func startGame(_ sender: Any) {
    let pressedButton : UIButton = sender as! UIButton
    pressedButton.isHidden = true;
}

您可以按照 Pratik 的建议稍微重写您的代码,因此它看起来像这样:

@IBAction func startGame(_ sender: UIButton) {
    sender.isHidden = true
    /*
    remove button at all from the parent view.
    sender.removeFromSuperview()
    */
    print("The game starts...")
}