【问题标题】:Swift 3 and unrecognized selector sent to instance error for button creation [duplicate]Swift 3和无法识别的选择器发送到实例错误以创建按钮[重复]
【发布时间】:2016-09-23 06:40:35
【问题描述】:

在我们开始使用 Swift 3 之后,我们遇到了这个“发送到实例的无法识别的选择器!错误,无法在合理的时间找到解决方案。我们遇到的旧版本 Swift 的所有答案。这是我们提出的行动宣言

     testbutton.addTarget(self, action: Selector("testButtonAction:"), for: UIControlEvents.touchDown)

这是 Xcode 的“更正”版本,但它们都不起作用。

     testbutton.addTarget(self, action: Selector(("testButtonAction:")), for: UIControlEvents.touchDown)

这是动作函数。

       func testButtonAction(sender: UIButton!) {

            print("Button tapped")

          }

【问题讨论】:

    标签: swift uibutton ios10


    【解决方案1】:

    Swift 3 的正确语法是:

    testbutton.addTarget(self, action: #selector(testButtonAction(sender:)), for: UIControlEvents.touchDown)
    

    这里的任何错误现在都会在编译时而不是运行时被发现。

    【讨论】:

      【解决方案2】:

      应该是这样的:

      testButton.addTarget(self, action: #selector(YourViewController.testButtonAction(sender:)), for: UIControlEvents.touchDown)
      

      【讨论】:

        猜你喜欢
        • 2021-01-04
        • 2017-08-30
        • 2018-04-24
        • 1970-01-01
        • 2017-01-23
        • 1970-01-01
        • 2018-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多