在MFMailComposeViewController中设置第一响应者?

cre*_*tic 6 email iphone iphone-sdk-3.0 mfmailcomposeviewcontroller

我正在使用Apple的MailComposer示例应用程序从我的应用程序中发送电子邮件(OS 3.0功能).是否可以使用MFMailComposeViewController将To,Subject或Body字段设置为第一响应者?

换句话说,行为将是:用户按下呈现邮件视图的按钮(presentModalViewController).显示邮件视图时,光标将放置在其中一个字段中,并且键盘将打开.

我注意到MFMailComposeViewController文档说:

"重要提示:邮件撰写界面本身不可自定义,不得由您的应用程序修改.此外,在显示界面后,您的应用程序不允许对电子邮件内容进行进一步更改.用户仍可以使用接口,但程序化更改被忽略.因此,您必须在呈现界面之前设置内容字段的值."

但是,我不关心自定义界面.我只想设置firstResponder.有任何想法吗?

hol*_*olz 8

您可以使这些字段成为第一个响应者.

如果你将以下方法添加到你的班级......

//Returns true if the ToAddress field was found any of the sub views and made first responder
//passing in @"MFComposeSubjectView"     as the value for field makes the subject become first responder 
//passing in @"MFComposeTextContentView" as the value for field makes the body become first responder 
//passing in @"RecipientTextField"       as the value for field makes the to address field become first responder 
- (BOOL) setMFMailFieldAsFirstResponder:(UIView*)view mfMailField:(NSString*)field{
    for (UIView *subview in view.subviews) {

        NSString *className = [NSString stringWithFormat:@"%@", [subview class]];
        if ([className isEqualToString:field])
        {
            //Found the sub view we need to set as first responder
            [subview becomeFirstResponder];
            return YES;
        }

        if ([subview.subviews count] > 0) {
            if ([self setMFMailFieldAsFirstResponder:subview mfMailField:field]){
                //Field was found and made first responder in a subview
                return YES;
            }
        }
    }

    //field not found in this view.
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

然后,在呈现MFMailComposeViewController之后,将MFMailComposeViewController的视图与您想要成为第一响应者的字段一起传递给该函数.

MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;

/*Set up the mail composer*/

[self presentModalViewController:mailComposer animated:YES];
[self setMFMailFieldAsFirstResponder:mailComposer.view mfMailField:@"RecipientTextField"];
[mailComposer release];
Run Code Online (Sandbox Code Playgroud)

  • 我相信由于使用了远程视图控制器,这种技术在iOS 6中不再适用.我很想知道实现这一目标的最新方法. (3认同)
  • 嗨,上面的解决方案对我不起作用,当我在方法中打印时,它只显示UINavigationTransitionView,UILayoutContainerView,UIButtonLabel,UINavigationButton等作为类名.它没有给出一个名为MFRecipientTextField等的子视图类.任何想法都错了吗? (2认同)

Kev*_*lar 1

您可以尝试仅在控制器本身上调用BecomeFirstResponder。如果这不起作用,您可以尝试在调试器中获取邮件撰写视图的子视图列表,直到找到熟悉的文本字段或文本视图,然后您可以专门对其进行编码以在代码中设置响应者状态,这可能看起来像这个(我不知道这是否有效,但这是一个例子):

[[[[mailcomposer.view.subviews objectAtIndex:3] subviews] objectAtIndex:2] becomeFirstResponder]
Run Code Online (Sandbox Code Playgroud)