Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knowing the complete changed string in textField:shouldChangeCharactersInRange:replacementString:

I just asked a question about how to monitor changes to a UITextField and received this response :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacementStr {     // Enable "Next" if you like what's entered in the replacementStr } 

This works, but the replacement string is not the whole string, just what it is adding. How can I get the whole string? My objective is to see if the string in the text field is blank or equal to a certain number (in different scenarios).

Please note that the outlet to the text field doesn't work in this case, because this method is being called before the text in the field changes.

like image 689
Redneys Avatar asked Jul 23 '12 00:07

Redneys


2 Answers

NSString * proposedNewString = [[textField text] stringByReplacingCharactersInRange:range withString:replacementString]; 
like image 200
jscs Avatar answered Oct 03 '22 04:10

jscs


Swift Version

In Swift We need to cast textField's text to NSString. The following can be useful:

let newString = (textField.text! as NSString).replacingCharacters(in: range, with: string) 
like image 20
Irfan Avatar answered Oct 03 '22 03:10

Irfan