I have a method, -(void)validateFormInput:(UITextField *)textField that gets called when the form’s “submit” button is pressed. The method does the following:
1) Validates text in a given textField
2) If invalid, pops up an alert box, draws a red border around the UITextField and returns focus to that UITextField.
Problem is I am not getting focus back to the invalid UITextField.
Here is the validation method – Why is this not putting focus back on the invalid UITextField?
Thanks!
//Called when form "submit" button is pressed
-(void)validateFormInput:(UITextField *)textField{
//If Validation fails...
if([textField.text length] <1){
//simple test case
textField.layer.borderWidth = 2.0f;
textField.layer.borderColor = [[UIColor redColor] CGColor];
textField.layer.cornerRadius = 5;
textField.clipsToBounds = YES;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Please add a title"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
alert = nil;
[textField becomeFirstResponder];
}
}
You need to add delegate to self and implement delegate method alertView:didDismissWithButtonIndex:. Dont forget to conform to UIAlertViewDelegate protocol.
When this method gets called make your textField first responder.