[英]textfield on touch not working
I am trying to set ontouch listener on UITextField but it not working at all here is my code:我正在尝试在 UITextField 上设置 ontouch 侦听器,但它根本不起作用,这是我的代码:
class SettingsVC: BaseVC {
@IBOutlet weak var languageField: SkyFloatingLabelTextField!
@IBOutlet weak var btnburgerMenu: UIButton!
@IBOutlet weak var headerLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
languageField.addTarget(self, action: "myTargetFunction:", for: UIControlEvents.touchDown)
}
func myTargetFunction(textField: SkyFloatingLabelTextField) {
print("test")
}
}
Here is the error I have这是我的错误
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[IFO.SettingsVC myTargetFunction:]: unrecognized selector sent to instance 0x7fac8b61ff30'
What's the reason of the exception?异常的原因是什么?
It should be like so -应该是这样——
languageField.addTarget(self, action: #selector(YourViewController.myTargetFunction(sender:)), for: UIControlEvents.touchDown)
or use like或使用像
languageField.addTarget(self, action: #selector(self.myTargetFunction(_:)), forControlEvents: .touchDown)
and call the function like并调用函数
func myTargetFunction(_ textField: SkyFloatingLabelTextField) {
print("test")
}
.touchDown is actually quite useless as it doesn't always fire. .touchDown 实际上非常无用,因为它并不总是触发。 I needed a way for dialog to appear every time UITextField was touched.
我需要一种每次触摸 UITextField 时都会出现对话框的方法。 I used this instead:
我用这个代替:
class DateTextField: DefaultTextField, UITextFieldDelegate {
...
override init() {
super.init()
self.delegate = self // delegate to handle textFieldShouldBeginEditing below
inputView = UIView() // disable the input view
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// Code to display my date picker dialog
return false // return false to disable textfield editing
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.