在 MFMailComposeViewController 中设置第一响应者?

发布于 2024-08-10 12:26:53 字数 548 浏览 10 评论 0原文

我正在使用 Apple 的 MailComposer 示例应用程序从我的应用程序中发送电子邮件(操作系统 3.0 功能)。是否可以使用 MFMailComposeViewController 将“收件人”、“主题”或“正文”字段设置为第一响应者?

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

我注意到 MFMailComposeViewController 文档说:

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

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

I'm using Apple's MailComposer example application to send email from within my application (OS 3.0 functionality). Is it possible to set the To, Subject, or Body fields as first responder with MFMailComposeViewController?

In other words, the behavior would be: the user presses a button which presents the mail view (presentModalViewController). When the mail view is presented, the cursor is placed in one of the fields and the keyboard opens.

I notice the MFMailComposeViewController documentation says:

"Important: The mail composition interface itself is not customizable and must not be modified by your application. In addition, after presenting the interface, your application is not allowed to make further changes to the email content. The user may still edit the content using the interface, but programmatic changes are ignored. Thus, you must set the values of content fields before presenting the interface."

However, I don't care about customizing the interface. I just want to set that firstResponder. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相权↑美人 2024-08-17 12:26:53

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

如果您将以下方法添加到您的类中...

//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];

You are able to make these fields become the first responder.

if you add the following method to your class...

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

Then, after you present the MFMailComposeViewController, pass the MFMailComposeViewController's view into the function along with the field you want to become first responder.

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];
伤感在游骋 2024-08-17 12:26:53

在 iOS 6 中,无法再在任何文本字段 AFAICT 上设置第一响应者。浏览视图层次结构最终会显示一个 UIRemoteView,并且此处的子视图被混淆掉。

In iOS 6, it is no longer possible to set first responder on any of the text fields AFAICT. Navigating the view hierarchy eventually reveals a UIRemoteView and the subviews within here are obfuscated away.

落墨 2024-08-17 12:26:53

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

[[[[mailcomposer.view.subviews objectAtIndex:3] subviews] objectAtIndex:2] becomeFirstResponder]

You can try just calling becomeFirstResponder on the controller itself. If that doesn't work, you can try in the debugger getting the list of subviews of the mail compose view until you find a familiar textfield or textview which you can then code specifically to set the responder status in code, which might look something like this (i don't know if this will work but it's an example):

[[[[mailcomposer.view.subviews objectAtIndex:3] subviews] objectAtIndex:2] becomeFirstResponder]
痴梦一场 2024-08-17 12:26:53

我喜欢简化代码并使其易于理解。
只需将以下代码放在后面即可:
[自我呈现ModalViewController:mailComposer动画:是];

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

I like to simplify the code and make it easy to understand.
Just put the follow code after:
[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.
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文