The text box should change characters in the string quickly

I am making an iPad app for learning English words. It should check the input in the text box as soon as characters are entered in the text box. I am using the quick shouldChangeCharactersInRange function to accomplish this.

My code is:

func textField(textField: UITextField, shouldChangeCharactersInRange range:NSRange, replacementString string: String) -> Bool { if TextField1.text == "apple" { checkImageView1.hidden = false } else { checkImageView1.hidden = true } return true } 

He needs to show the image if the word is printed correctly, in this case "apple". The problem is that when the user enters "e" from the "apple", the check only sees "appl" and therefore does not show the image.

Does anyone know how to solve this?

+6
source share
5 answers

You can use the target on your textField with the EditingChanged control EditingChanged instead of the delegate method.

Swift> = 1.0

 myTextField.addTarget(self, action: "didChangeText:", forControlEvents: .EditingChanged) 

Swift 3.0 (string literal selectors are deprecated, use #selector)

 myTextField.addTarget(self, action: #selector(didChangeText(_:)), for: .editingChanged) 

Then use the target method to perform the checks.

 func didChangeText(textField:UITextField) { if textField.text == "apple" { checkImageView1.hidden = false } else { checkImageView1.hidden = true } } 
+15
source

While Wezly's answer is correct, the reason he sees "appl" instead of "apple" is because the textField text textField not updated with the last record at this point, since the delegate name shouldChangeCharactersInRange . Instead of checking the text textField , you should replace the new record and check the new value as follows:

Swift 3

 let newText = textField.text?.replacingCharacters(in: range, with: string) 

Using the EditingChanged event will work if you just want to get the last value, but if you want to do some verification of each record before , the text will actually be edited, you need to go with the delegate method.

+3
source

Instead of using the delegate method, attach the method to the Edit Edited Value event and accept the text filter as the sender.

In the viewDidLoad method:

 textField.addTarget(self, action: "textFieldEdited:", forControlEvents: UIControlEvents.EditingChanged) 

This is the method to be activated.

 func textFieldEdited(sender: UITextField) { var text = sender.text } 
+2
source

Use the following code. This function gets the new string value added to the string variable. If you connect it to textField1.text, you will get the full line in textField.

  func textField(textField: UITextField, shouldChangeCharactersInRange range:NSRange, replacementString string: String) -> Bool { var startString = "" + TextField1.text+string //Now this should be 'Apple' return true } 
+2
source

Swift 3 As correctly said to chengs: replacingCharacters in: with: need a NSString . So you need to do something like this:

 let newText = NSString(string: textField.text!).replacingCharacters(in: range, with: string) 
+2
source

Source: https://habr.com/ru/post/985055/


All Articles