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

拥有回忆 提交于 2019-12-24 05:45:11

问题


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.


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/39509556/value-of-type-custombutton-has-no-member-touchdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!