0

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.

Bhavik P.
  • 905
  • 3
  • 10
  • 28

1 Answers1

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)
Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
  • I got an error message saying Cannot convert value of type 'NSRange' (aka '_NSRange') to expected type 'Range'(aka 'Range'). Googled it and it lead me to http://stackoverflow.com/questions/32769753/cannot-convert-value-of-type-nsrange-aka-nsrange-to-expected-type-range Problem is fixed. @Clever Error, you da man! Thanks! – Bhavik P. Nov 16 '15 at 22:44