2

i did a simple application with two text fields.

I need to print in the second textfield same as which is enter in first text field simultaneously.

For that i am writing the fallowing code.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{ 
   [textfield2 setText:textfield1.text]; 

}

but it prints from second value.

for example text field1 = 111;

it is displaying in text field as 11;

what is the wrong.

i need what exactly enter in text field1 to text field2 simultaneously.

can any one pls help me.

Thank u in advance.

Mahesh Babu
  • 3,395
  • 9
  • 48
  • 97
  • 2
    @MaheshBabu As @EvanMulawski pointed out, you're using the wrong event handler. The one you're using, `shouldChangeCharactersInRange`, happens **before** the first text field changed, which is why it starts with "shouldChange", which is future tense in English, instead of "changed," which is past tense in English. – Matthew Frederick Dec 15 '10 at 15:51

2 Answers2

4

Set Event Handler (or use Interface Builder):

[textField1 addTarget:self 
               action:@selector(textFieldEditingDidChange:)
     forControlEvents:UIControlEventEditingChanged];

Method:

- (void)textFieldEditingDidChange:(UITextField *)sender
{
    textField2.text = sender.text;
}

When the value of the text field is changed, it calls the method above, which sets the value of the second text field.

Code altered from: Getting the Value of a UITextField as keystrokes are entered?

Community
  • 1
  • 1
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
0

You could do this

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{ 
    NSMutableString *localString = textfield2.text;
     if([string isEqualToString:@""]){
         [localString replaceCharactersInRange:NSMakeRange([localString length]-1, 1) 
                                    withString:@""];
     }else{
         [localString appendString:string];
     }
     textfield2.text = localString;

}

:D

Alex Terente
  • 12,006
  • 5
  • 51
  • 71