[英]Textfield shouldchangecharactersinrange swift
I am making an iPad-app to learn english words. 我正在制作一个iPad应用程序来学习英语单词。 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.
我正在使用swift函数shouldChangeCharactersInRange来完成此任务。
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". 如果单词输入正确,则需要显示图像,在本例中为“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.
问题是当用户键入“苹果”的“e”时,检查仅看到“appl”,因此不显示图像。
Anyone know how to solve this? 有谁知道如何解决这个问题?
You could use a target on your textField
with the control event EditingChanged
instead of the delegate method. 您可以使用控件事件
EditingChanged
而不是委托方法在textField
上使用目标。
Swift >= 1.0 Swift> = 1.0
myTextField.addTarget(self, action: "didChangeText:", forControlEvents: .EditingChanged)
Swift 3.0 (String literal selectors are deprecated, use #selector) Swift 3.0 (不推荐使用字符串文字选择器,使用#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
}
}
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
. 虽然Wezly的答案是正确的,但是它只看到“appl”而不是“apple”的原因是因为
textField
的文本没有使用最新的条目更新,因为委托方法的名称应该是来自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: 您应该更换新条目并使用以下内容检查新值,而不是检查
textField
的文本:
Swift 3 斯威夫特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. 如果您只想获取最新值,则使用事件
EditingChanged
将起作用,但如果您想在实际编辑文本之前对每个条目进行一些验证,则必须使用委托方法。
Instead of using the delegate method, attach a method to the event "Editing Value Changed" and accept the textField as the sender. 而不是使用委托方法,将方法附加到事件“编辑值已更改”并接受textField作为发件人。
In the viewDidLoad method: 在viewDidLoad方法中:
textField.addTarget(self, action: "textFieldEdited:", forControlEvents: UIControlEvents.EditingChanged)
This is the method which will get activated 这是将被激活的方法
func textFieldEdited(sender: UITextField)
{
var text = sender.text
}
Use the following code. 使用以下代码。 This function gets the new string value being added in variable
string
. 此函数获取在变量
string
中添加的新字符串值。 If you concatenate it with textField1.text, you will get full string in textField. 如果将它与textField1.text连接,您将在textField中获得完整的字符串。
func textField(textField: UITextField, shouldChangeCharactersInRange range:NSRange, replacementString string: String) -> Bool {
var startString = "" + TextField1.text+string //Now this should be 'Apple'
return true
}
Swift 3 As chengsam correctly mentioned: replacingCharacters in: with:
needs a NSString
. Swift 3正如chengsam正确提到的: replacementCharacters
replacingCharacters in: with:
需要一个NSString
。 So you need to do something like this: 所以你需要做这样的事情:
let newText = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.