You should assign a delegate to your UITextField
s.
Your datasource (or any other object, but this one seems ok for the job) should conform to <UITextFieldDelegate>
protocol.
...
nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
[nameTextField setBackgroundColor:[UIColor clearColor]];
[nameTextField setFont:[UIFont fontWithName:@"Arial" size:14]];
nameTextField.placeholder = @"Enter anonymous name";
[nameTextField setKeyboardType: UIKeyboardTypeNamePhonePad];
[nameTextField setDelegate:self]; //add this line
[cell addSubview:nameTextField];
//[nameTextField resignFirstResponder]; this doesn't really make sense - creating the text
//field should not (and does not) bring up the keyboard
[nameTextField release];
....
Do the same for other UITextField
s.
Then check if your delegate
method gets called (using NSLog for example, or breakpoints):
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog (@"should return?");
[textField resignFirstResponder];
return YES;
}
EDIT:
You only have to set the delegate once for each UITextField - usually when it's created.
To tell the compiler that a certain class confroms to <UITextFieldDelegate>
protocol just add after the class @interface
declaration in header file.
For example, if the class that you use as a dataSource for the table is named MyDataSource:
@interface MyDataSource: NSObject <UITableViewDelegate,UITextFieldDelegate>
Of course you have to implement all the required methods to correctly conform to a certain protocol. Please check the documentation: UITextFieldDelegate Protocol Reference
EDIT: as for checking the validitiy of email: you can check the validity (this is actually another question) you can do this in - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Since your delegate object is delegate to more then one UITextField you'll have to check first if text field being edited is the one holding the email. And since your text fields are in table cells it would be hard (and unceccessary) to hold reference to all of them. You can separate email text fields from the others by using tag - let's make it 44. Modify your code slightly:
emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 18, 300, 20)];
emailTextField.tag = 44; //add this line
Then add the delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag == 44)
{
//email checking code here
}
else
{
return YES;
}
}
As for how to check email, take a look at this answer: https://stackoverflow.com/a/7707946/653513