【问题标题】:How to hide an action button in Swift?如何在 Swift 中隐藏操作按钮?
【发布时间】:2018-10-16 12:10:29
【问题描述】:

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

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

}

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

startGame.isHidden = true 

是否有其他方法可以使操作按钮不可见,因此不可点击?

【问题讨论】:

  • 试试sender.isHidden = true
  • 从技术上讲,什么是操作按钮
  • @PratikPrajapati sender.isHidden = true 不幸的是也不起作用
  • _ sender: Any 更改为 _ sender: UIButton n 再试一次

标签: swift xcode iboutlet ibaction


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2019-02-02
        • 2021-01-27
        • 2016-07-27
        • 2015-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-13
        相关资源
        最近更新 更多