【问题标题】:Only instance methods can be declared @IBAction?只有实例方法可以声明@IBAction?
【发布时间】:2019-11-14 15:24:19
【问题描述】:

只能声明实例方法@IBAction - 这是什么意思?

【问题讨论】:

  • 因为一个动作的目标必须是一个对象,也就是一个类的实例。因此,方法(动作)必须声明为实例方法。

标签: swift xcode


【解决方案1】:

意味着你不能使class/static方法@IBAction可能是

@IBAction class func aaaa() { // Error here 
    ////
}

【讨论】:

    【解决方案2】:

    实例方法就是人们通常所说的“方法”。

    class myVC: UIViewcontroller {
    
    
      //this won't work
    
      @IBAction static func buttonTapped(_ sender: UIButton) {
         //...
      }
    
      //whereas this will
    
      @IBAction func buttonTapped(_ sender: UIButton) {
        //...
      }
    }
    

    【讨论】:

    • @IBAction func actionButton1(_ sender: Any) { if fraga.randomElement() == "一个巨无霸有多高" { answer1.backgroundColor = UIColor.red s 当我尝试这个时还是说只有实例方法可以声明IBAction
    • 对我来说,您的班级中的括号不平衡,因此它实际上并不在您认为的班级层次结构中。也许将课程添加到问题中?
    • 你有意见不和吗,那里讨论比较容易。
    • 我也可以发送不和谐的照片,你介意吗?
    • 怕我不会用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多