本文介绍了“CustomButton"类型的值没有成员“touchDown"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :

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

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

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

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

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

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

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

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

Value of type 'CustomButton' has no member '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
    }
}

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

If anyone have any solution, please let me know.

推荐答案

如果你想在你的代码中保留你的方法头,你需要将选择器引用更改为 #selector(touchDown(sender:)),等等.

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.

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

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

请记住,所有函数和方法现在对它们的第一个参数都有一致的标签处理.SE-0046

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

(你可能会发现很多好文章,用swift3选择器"搜索.)

(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) {

此外,如果您的类只有一个 touchDown(...) 方法,#selector(touchDown) 会起作用.

这篇关于“CustomButton"类型的值没有成员“touchDown"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 09:12