Value of type 'CustomButton' has no member

2019-07-29 10:10发布

I have created custom button class and override all touches methods. It works fine in swift 2 and Xcode 7.3.1. But when I open same app in Xcode 8.0, it will show errors :

Value of type 'CustomButton' has no member 'touchDown'

Value of type 'CustomButton' has no member 'touchUpInside'

Value of type 'CustomButton' has no member 'touchDragExit'

Value of type 'CustomButton' has no member 'touchDragEnter'

Value of type 'CustomButton' has no member 'touchCancel'

Here is my code :

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
    }
}

If anyone have any solution, please let me know.

2条回答
The star\"
2楼-- · 2019-07-29 10:33

If you want to keep your method headers as are in your code, you need to change the selector references to #selector(touchDown(sender:)), and so on.

(Generally, you have no need to prefix self..)

Remember all functions and methods now have consistent label treatment for their first parameters. SE-0046

(You may find many good articles, searching with "swift3 selector".)

If you want to keep the selector references, you need to change the methods like:

func touchDown(_ sender: CustomButton) {

For addition, #selector(touchDown) would work, if your class has only one touchDown(...) method.

查看更多
时光不老,我们不散
3楼-- · 2019-07-29 10:35

I have found solution as @OOPer suggested, but there also need to change UIControlEvents in small case. In Xcode 7.3.1 it was UIControlEvents.TouchDown but now it has to be UIControlEvents.touchDown.

It will be like :

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)
查看更多
登录 后发表回答