alecnash
alecnash

Reputation: 1768

multiple UITextFields on one view

I've got a little problem with uitextfield. I use two of them in a view and when I write something to the second field the first string gets changed too. Here is my code

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

textString = textField.text;
NSLog(@"the string1 %@",textString);
[textField resignFirstResponder];


textString2 = textField2.text;
NSLog(@"the string2 %@",textString2);
[textField2 resignFirstResponder];

return YES;}

so I need some help.

Upvotes: 2

Views: 3080

Answers (2)

Swapnil Luktuke
Swapnil Luktuke

Reputation: 10475

Your textFieldShouldReturn method gets called for both the fields so you need to distinguish between the actions for each field.

Set a tag to both the text fields:

myTextField1.tag = 100;
myTextField2.tag = 101;

and check for the tag in the textFieldShouldReturn method:

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField.tag == 100)
    {
        textString = textField.text;
    }
    else if(textField.tag == 101)
    {
        textString2 = textField.text;
    }

    [textField resignFirstResponder];
}

Here textField is the object passed to the delegate i'e' the one on which you have tapped return. So use that and not your IBOutlet object.

And please avoid naming your text fields textField and textField2 its a very bad coding practice.

Good Luck

Upvotes: 11

Roxy Light
Roxy Light

Reputation: 5147

Sounds like your IBOutlets might be pointing to the same object. Re-check your connections in Interface Builder.

Upvotes: 0

Related Questions