1

I have a class where I used the textfield delegate method "shouldChangeCharactersInRange". If the user types something in a textfield in one view, I want those changes to appear in a textfield in a different view. Right I now, I have two xib files with the same file's owner and I make a connection (in IB) in each xib file to my textfield (which i declared as an IBOutlet). It's an Ipad app so I switch between views when user rotates device. It's not working yet so I must be missing something? Could someone please help me! thank you! If this question is not clear, please let me know.

serge2487
  • 441
  • 2
  • 9
  • 23
  • You should edit your question and add what you have in shouldChangeCharactersInRange method. This way we can see what you are talking about. – Black Frog Apr 05 '11 at 19:31
  • I see what you mean but I dont thing it matters what happens in that method, i just want the same to happen in my other textfield. – serge2487 Apr 05 '11 at 19:43
  • 2
    You know where they say a picture is worth a thousand words? Well code it worth 10x as much. – Black Frog Apr 05 '11 at 19:56

1 Answers1

0

Wherever your shouldChangeCharactersInRange: method is implemented, if you have a reference to both of the textfields, what you can do is set the text of both. So where right now you have something like:

- (BOOL)textField: (UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string
{
    if (textField.text.length >= MAX_LENGTH && range.length == 0)
    {
        return NO; 
    }
    else
    {
        return YES;
    }
}

You want to add something like:

- (BOOL)textField: (UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string
{
    if (textField.text.length >= MAX_LENGTH && range.length == 0)
    {
        return NO; 
    }
    else
    {   
        [myFirstTextField setText:string];
        [mySecondTextField setText:string];
        return YES;
    }
}

And if you set both textFields to delegate to that single function, you don't even need to care who delegated to you there. The action you want to take is the same no matter who got text entered in them.

Nektarios
  • 10,173
  • 8
  • 63
  • 93
  • i only have one textfield name because in both of my xib files, both of my textfields are connected to the same name and have the same delegate and tag. – serge2487 Apr 05 '11 at 19:51
  • 1
    That's ok - you still have 2 separate objects even though they have the same name. They are instantiated because they are in separated xibs. It doesn't matter what they are called. The important thing is that you get a REFERENCE to each of them back to their delegate. – Nektarios Apr 06 '11 at 03:12