【问题标题】:How to disable a button within a anyObject Button in swift如何在swift中禁用anyObject按钮中的按钮
【发布时间】:2021-04-22 16:57:32
【问题描述】:

好的,所以我希望能够禁用按钮本身。所以当我按下按钮时,它会自行禁用。

import UIKit

class ViewController: UIViewController {

    @IBOutlet var buttons: [UIButton]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func but(_ sender: AnyObject) {
//this code doesn't seem to work at all, and I don't know why
        (sender as AnyObject).isEnabled = false
    }
   
}

当我尝试执行此操作时,它会给我一条错误消息“无法分配给'@lvalue Bool'类型的不可变表达式?”

那么有没有其他方法可以禁用此按钮

【问题讨论】:

    标签: swift button


    【解决方案1】:

    大概,您已经在 Interface Builder 中将这个 IBAction 连接到了 UIButton。因此,您可以将函数签名更改为:

    @IBAction func but(_ sender: UIButton) {
      sender.isEnabled = false
    }
    

    AnyObject 没有名为isEnabled 的属性,这就是您之前的代码失败的原因。

    【讨论】:

      【解决方案2】:

      在按钮内我使用此代码

      @IBAction func but(_ sender: AnyObject) {
              var disableMyButton = sender as? UIButton
              disableMyButton?.isEnabled = false
             
          }
      

      【讨论】:

        猜你喜欢
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多