“CustomButton”类型的值没有成员“touchDown”

时间:2016-09-15 11:07:26

标签: uibutton swift3 xcode8

我创建了自定义按钮类并覆盖了所有触摸方法。它在swift 2Xcode 7.3.1中运行良好。但是当我在Xcode 8.0中打开相同的应用时,它会显示错误:

  

'CustomButton'类型的值没有成员'touchDown'

     

'CustomButton'类型的值没有成员'touchUpInside'

     

'CustomButton'类型的值没有成员'touchDragExit'

     

'CustomButton'类型的值没有成员'touchDragEnter'

     

'CustomButton'类型的值没有成员'touchCancel'

这是我的代码:

import UIKit

@IBDesignable
@objc public class CustomButton: UIButton {

    private func addTargets() {

        //------ add target -------

         self.addTarget(self, action: #selector(self.touchDown(_:)), for: UIControlEvents.TouchDown)
         self.addTarget(self, action: #selector(self.touchUpInside(_:)), for: UIControlEvents.TouchUpInside)
         self.addTarget(self, action: #selector(self.touchDragExit(_:)), for: UIControlEvents.TouchDragExit)
         self.addTarget(self, action: #selector(self.touchDragEnter(_:)), for: UIControlEvents.TouchDragEnter)
         self.addTarget(self, action: #selector(self.touchCancel(_:)), for: UIControlEvents.TouchCancel)
    }

    func touchDown(sender: CustomButton) {
       self.layer.opacity = 0.4
    }

    func touchUpInside(sender: CustomButton) {
       self.layer.opacity = 1.0
    }

    func touchDragExit(sender: CustomButton) {
       self.layer.opacity = 1.0
    }

    func touchDragEnter(sender: CustomButton) {
       self.layer.opacity = 0.4
    }

    func touchCancel(sender: CustomButton) {
        self.layer.opacity = 1.0
    }
}

如果有人有任何解决方案,请告诉我。

2 个答案:

答案 0 :(得分:1)

如果您希望将方法标题保留在代码中,则需要将选择器引用更改为#selector(touchDown(sender:)),依此类推。

(通常,您无需为self.添加前缀。)

请记住,所有功能和方法现在都对其第一个参数进行了一致的标签处理。 SE-0046

(您可以找到许多好文章,使用“swift3选择器”进行搜索。)

如果要保留选择器引用,则需要更改以下方法:

func touchDown(_ sender: CustomButton) {

另外,如果您的班级只有一个#selector(touchDown)方法,则touchDown(...)会有效。

答案 1 :(得分:0)

我找到了@OOPer建议的解决方案,但还需要在小的情况下更改curl -X POST http://x.x.x.x:8080/iot/services \ -i \ -H "Content-Type: application/json" \ -H "Fiware-Service: service2" \ -H "Fiware-ServicePath: /srvpath2" \ -d '{"services": [{ "apikey": "apikey2", "token": "token2", "cbroker": "http://127.0.0.1:1026", "entity_type": "thing", "resource": "/iot/d" }]}' UIControlEvents Xcode 7.3.1UIControlEvents.TouchDown,但现在必须为UIControlEvents.touchDown

就像:

self.addTarget(self, action: #selector(touchDown(sender:)), for: UIControlEvents.touchDown)
self.addTarget(self, action: #selector(touchUpInside(sender:)), for: UIControlEvents.touchUpInside)
self.addTarget(self, action: #selector(touchDragExit(sender:)), for: UIControlEvents.touchDragExit)
self.addTarget(self, action: #selector(touchDragEnter(sender:)), for: UIControlEvents.touchDragEnter)
self.addTarget(self, action: #selector(touchCancel(sender:)), for: UIControlEvents.touchCancel)