I have a Ui text field and when the user stops typing after about 1 second I want a check mark to appear. I have the following code:
@objc func getHintsFromTextField(textField: UITextField) {
print("Hints for textField: \(textField)")
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
NSObject.cancelPreviousPerformRequests(
withTarget: self,
selector: #selector(ViewController.getHintsFromTextField),
object: textField)
self.perform(
#selector(ViewController.getHintsFromTextField),
with: textField,
afterDelay: 3.0)
return true
}
``` @IBAction func password3(_ sender: UITextField) {
if textField(passwordSignUp, shouldChangeCharactersIn: NSMakeRange(5, 10), replacementString: "") == true {
let imageView = UIImageView()
let image1 = #imageLiteral(resourceName: "icons8-ok-28")
imageView.image = image1
self.passwordSignUp?.rightView = imageView
self.passwordSignUp?.rightViewMode = UITextField.ViewMode.always
self.passwordSignUp?.rightViewMode = .always
errorLabel?.alpha = 0
}
}
(By the way the IBAction func password3 is on start typing) When I run my code as soon as I tap the text field the check mark appears. What am I doing wrong.
I would use something like:
func textFieldDidChangeSelection(_ textField: UITextField) {
checkmarkImageView.isHidden = True
let timer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true)
}
And:
@objc func fireTimer() {
checkmarkImageView.isHidden = False
}
Instead of creating the imageView each time, set it up as a IBOutlet that's initially hidden.