无法识别的选择器发送到实例,原因:' - [UITouchesEvent rightView]

时间:2016-08-04 11:43:08

标签: swift unrecognized-selector

我的应用崩溃并抛出此错误

[UITouchesEvent rightView]: unrecognized selector sent to instance 0x13d5092e0
2016-08-04 17:07:15.569 [3809:1375151] 

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITouchesEvent rightView]: unrecognized selector sent to instance 0x13d5092e0'

*** First throw call stack:
(0x180d72db0 0x1803d7f80 0x180d79c4c 0x180d76bec 0x180c74c5c 0x1000e681c 0x1000e6a60 0x185f08be8 0x185f08b64 0x185ef0870 0x185f11360 0x185f07ed8 0x185f00c20 0x185ed104c 0x185ecf628 0x180d2909c 0x180d28b30 0x180d26830 0x180c50c50 0x182538088 0x185f3a088 0x1000f5c88 0x1807ee8b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

我正在尝试为隐藏和显示密码创建自定义类,以便我可以在任何UITextField中的UIViewController中使用它。

class HideShowIcon:NSObject {

    var showPasswordImage = UIImage(named: "ic_show_password") as UIImage?
    var hidePasswordImage = UIImage(named: "ic_hide_password") as UIImage?


    func hideShowPasswordButton(hideText:UITextField) {

        var hideShowSize: CGSize = "12345".sizeWithAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14.0)])
        var hideShow: UIButton = UIButton(type: UIButtonType.System)
        hideShow.frame = CGRect(x: 0, y: 0, width: hideShowSize.width, height: hideText.frame.size.height)
        hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
        hideText.rightView = hideShow
        hideText.rightViewMode = UITextFieldViewMode.Always
        hideShow.addTarget(self, action: #selector(HideShowIcon.hideShowPasswordTextField(_:hideText:)), forControlEvents: UIControlEvents.AllTouchEvents)

    }
    func hideShowPasswordTextField(sender: AnyObject,hideText:UITextField) {

        var hideShow: UIButton = (hideText.rightView as? UIButton)!
        if !hideText.secureTextEntry {
            hideText.secureTextEntry = true

            hideShow.setImage(hidePasswordImage, forState: UIControlState.Normal)
        } else {
            hideText.secureTextEntry = false
            hideShow.setImage(showPasswordImage, forState: UIControlState.Normal)
        }
        hideText.becomeFirstResponder()
    }
}

1 个答案:

答案 0 :(得分:0)

对于hideShowPasswordTextField(_:hideText:)目标,您不能使用包含许多参数UIButton的选择器。您只能使用带有UIButton自身的一个(或零)参数的选择器:hideShowPasswordTextField(_:)

func hideShowPasswordTextField(sender: UIButton) {
    //...
}

要在此功能中使用hideText,您可以将其声明为您班级的属性:

var hideText: UITextField!

并在类初始化步骤

中为其赋值