views:

750

answers:

3

//find below, the code example & associated result

- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {    
  if (theTextField == textField1){    
     [textField2 setText:[textField1 text]];    
  }
}

This code produces...

textField2 is "12", when textField1 is "123"

textField2 is "123", when textField1 is "1234"

... when what I want is:

textField2 is "123", when textField1 is "123"

textField2 is "1234", when textField1 is "1234"

+3  A: 

-shouldChangeCharactersInRange gets called before text field actually changes its text, that's why you're getting old text value. To get the text after update use:

[textField2 setText:[textField1.text stringByReplacingCharactersInRange:range withString:string]];
Vladimir
You are the best. Thanks.
A: 

Instead of using the UITextFieldDelegate, try to use "Editing Changed" event of UITextField.
http://stackoverflow.com/questions/388237/getting-the-value-of-a-uitextfield-as-keystrokes-are-entered

tomute
A: 

This is the code you need,

if ([textField isEqual:self.textField1])
  textField2.text = [textField1.text stringByReplacingCharactersInRange:range withString:string];
Deepak