Thursday 3 May 2012

How to set To Field first responder in MFMailComposeViewController


The function below is used to set mail composer fields first responder i.e when mail opens it comes with keyboard and focus is on one of the fields you want. I searched many other places but i was not satisfied. i used this code in my app. And its working great. Here INDEX is the field you want to set as first responder. 

Index=0=TO= Recipient
Index=1=CC
Index=2Bcc
Index=3=Subject


It should be global. The view you will pass should be MFMailComposeViewControllerObj.view .


-(void)setFirstrResponder:(UIView *)view
{  
    for(UIView *subview in view.subviews)
    {
            NSString *className = [NSString stringWithFormat:@"%@", [subview class]];
            if ([className isEqualToString:@"_MFMailRecipientTextField"])
            {
                if(index==0)
                {
                          index++;
                         [subview becomeFirstResponder];
                  return;
                }
           }
           else
            {
                          [self setFirstrResponder:subview];
             }
    }
}

1 comment:

  1. -(BOOL)setFirstrResponder:(UIView *)view
    {
    for(UIView *subview in view.subviews)
    {
    NSString *className = [NSString stringWithFormat:@"%@", [subview class]];
    if ([className isEqualToString:@"_MFMailRecipientTextField"])
    {
    [subview becomeFirstResponder];
    return YES;
    }
    else
    {
    if([self setFirstrResponder:subview]) break;
    }
    }
    return NO;
    }

    ReplyDelete