6

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

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

我注意到 MFMailComposeViewController 文档说:

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

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

4

4 回答 4

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;
}

然后,在您呈现 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];
于 2010-07-13T01:42:08.263 回答
4

在 iOS 6 中,不再可能在任何文本字段 AFAICT 上设置第一响应者。导航视图层次结构最终会显示一个 UIRemoteView 并且这里的子视图被混淆了。

于 2013-03-09T14:23:49.873 回答
1

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

[[[[mailcomposer.view.subviews objectAtIndex:3] subviews] objectAtIndex:2] becomeFirstResponder]
于 2010-02-12T17:03:12.237 回答
0

我喜欢简化代码并使其易于理解。只需将以下代码放在后面:
[self presentModalViewController:mailComposer animated:YES];

for (UIView *subview in mailComposer.view.subviews) {
   NSString *className = [NSString stringWithFormat:@"%@", [subview class]];
   //NSLog(@"%@", className); // list the views - Use this to find another view
   //The view I want to set as first responder: "_MFMailRecipientTextField"
   if ([className isEqualToString:@"_MFMailRecipientTextField"]){
   [subview becomeFirstResponder];
   break; // Stop search.
  }
}
于 2012-04-04T01:05:30.417 回答