TextField delegate shouldChangeCharactersInRange

。_饼干妹妹 提交于 2021-01-21 11:54:12

问题


Why doesn't the shouldChangeCharactersInRange update the textfield right away. It is 1 delayed by one character.

For example, download this file: https://www.dropbox.com/s/goljs3d6lcxutxy/UITextField%20Tutorial.zip?dl=0

add the following code to

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

method

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.


回答1:


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)


来源:https://stackoverflow.com/questions/33745428/textfield-delegate-shouldchangecharactersinrange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!