views:

64

answers:

3

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.

A: 

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

Quartz
found it thanks though
alecnash
A: 

found it! I shouldn't have named it

-(BOOL)textFieldShouldReturn:(UITextField *)textField

cause this (UITextField *)textField gets mixed with the textField.text; inside.I just changed its name.

alecnash
A: 

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

lukya
I changed the (UITextField *)textField into (UITextField *)text and it worked fine. so you are right by telling me that it bad coding practice to name them like that
alecnash
well good that your problem is solved, but what i meant by bad coding practice is that your object names should be intuitive like 'userNameTextFiled', 'passwordTextFiled' etc. so that even is someone else looks at your code (or even if you see it after an year) they understand whats happening. so i would say naming it 'text' as you did isn't that great either.. sorry if i'm bugging you with the lecture though..
lukya