gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:10:43 24 4
gpt4 key购买 nike

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

[UITouchesEvent rightView]: unrecognized selector sent to instance 0x13d5092e02016-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

I'm trying to make a custom class for hide and show password so that I can use it in any UITextField in any 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()
}
}

最佳答案

对于 UIButton 目标,您不能使用带有多个参数 hideShowPasswordTextField(_:hideText:) 的选择器。您只能使用带有一个(或零个)参数的选择器,UIButton 会将其自身放入其中:hideShowPasswordTextField(_:)

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

要在此函数中使用 hideText,您可以将其声明为类的属性:

var hideText: UITextField!

并在类初始化步骤中为其赋值

关于swift - 无法识别的选择器发送到实例,原因 : '-[UITouchesEvent rightView],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38766399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com