TextField 委托 shouldChangeCharactersInRange [英] TextField delegate shouldChangeCharactersInRange
问题描述
为什么 shouldChangeCharactersInRange 不立即更新文本字段.1 延迟一个字符.
Why doesn't the shouldChangeCharactersInRange update the textfield right away. It is 1 delayed by one character.
例如,下载此文件:https://www.dropbox.com/s/goljs3d6lcxutxy/UITextField%20Tutorial.zip?dl=0
将以下代码添加到
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
方法
print(textField.text)
看看它是如何延迟一个字符的.我希望它在我输入文本时更新而不是延迟.
see how it is delayed by one character. I want it to be updated as I type the text not one delayed.
推荐答案
该方法询问您是否应该采用 replacementString
并将其添加到 textField.text
.在你按下键盘上的一个键之后,屏幕上出现字母之前,它会立即被调用.
That method is asking you if it should take the replacementString
and add it onto textField.text
. It is called immediately after you press a key on the keyboard and before letter appears on screen.
要查看新字符串将是什么,您需要这样做.
To see what the new string will be, you'd need to to something like this.
let newText = textField.text.stringByReplacingCharactersInRange(range, withString: string)
print(newText)
这篇关于TextField 委托 shouldChangeCharactersInRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!