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

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. :pensive:

This works:

    @objc func touchDown(sender: CustomButton) {
        self.layer.opacity = 0.4
    }
    /*...*/
    func addTargets() {
        
        //------ add target -------
        self.addTarget(self, action: #selector(self.touchDown(sender:)), for: .touchDown)

Looks like making the actions @objc gives visibility, and autocomplete now offers a slightly different style for #selector. Also, note .touchDown now begins with ‘t’ not ‘T’.

1 Like