问题
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