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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With