A value of type "CustomButton" has no contact "touchDown"

I created my own button class and overrides all touch methods. It works great in swift 2 and Xcode 7.3.1 . But when I open the same application in Xcode 8.0 , it will show errors:

A value of type "CustomButton" does not have a "touchDown" member.

A value of type "CustomButton" has no contact "touchUpInside"

A value of type 'CustomButton' does not have a touchDragExit username '

A value of type "CustomButton" has no relation to the "touchDragEnter" member

A value of type "CustomButton" does not have a contact "touchCancel" member

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 has any solution, please let me know.

+1
swift3 xcode8 uibutton
Sep 15 '16 at 11:07
source share
2 answers

If you want to save the headers of your methods in your code, you need to change the selector links to #selector(touchDown(sender:)) , etc.

(Generally, you do not need the self. prefix.)

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

(You can find many good articles, search with "swift3 selector".)

If you want to save selector links, you need to change the following methods:

 func touchDown(_ sender: CustomButton) { 



To add, #selector(touchDown) will work if your class has only one touchDown(...) method.

+1
Sep 15 '16 at 11:29
source share

I found the solution suggested by @OOPer, but I also need to modify UIControlEvents in a small case. In Xcode 7.3.1 it was UIControlEvents.TouchDown , but now it should be UIControlEvents.TouchDown .

It will look like this:

 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) 
0
Sep 15 '16 at 12:33
source share



All Articles