可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am making an iPad-app to learn english words. It needs to check the input in a textfield as soon as the characters are typed in the textfield. I am using the swift function shouldChangeCharactersInRange to accomplish this.
My code:
func textField(textField: UITextField, shouldChangeCharactersInRange range:NSRange, replacementString string: String) -> Bool { if TextField1.text == "apple" { checkImageView1.hidden = false } else { checkImageView1.hidden = true } return true }
It needs to show an image if the word is typed right, in this case "apple". The problem is that when te user types in the "e" of "apple" the check only sees "appl" and therefor doesn't show the image.
Anyone know how to solve this?
回答1:
You could use a target on your textField
with the control event 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 targeted method to run your checks.
func didChangeText(textField:UITextField) { if textField.text == "apple" { checkImageView1.hidden = false } else { checkImageView1.hidden = true } }
回答2:
While Wezly's answer is correct, the reason why it's only seeing "appl" instead of "apple" is because the text of the textField
isn't updated with the latest entry at that point, as the name of the delegate method states shouldChangeCharactersInRange
. Instead of checking the text of the textField
, you should do the replacement of the new entry and check the new value with something like below:
Swift 3
let newText = textField.text?.replacingCharacters(in: range, with: string)
Using the event EditingChanged
would work if you just want to get the latest value, but if you want to do some validation of each entry before the text is actually edited, you have to go with the delegate method.
回答3:
Instead of using the delegate method, attach a method to the event "Editing Value Changed" and accept the textField as the sender.
In the viewDidLoad method:
textField.addTarget(self, action: "textFieldEdited:", forControlEvents: UIControlEvents.EditingChanged)
This is the method which will get activated
func textFieldEdited(sender: UITextField) { var text = sender.text }
回答4:
Use the following code. This function gets the new string value being added in variable string
. If you concatenate it with textField1.text, you will get full string 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 }
回答5:
Swift 3 As chengsam correctly mentioned: replacingCharacters in: with:
needs a NSString
. So you need to do something like this:
let newText = NSString(string: textField.text!).replacingCharacters(in: range, with: string)