我正在使用Apple的MailComposer示例应用程序从我的应用程序中发送电子邮件(OS 3.0功能)。是否可以使用MFMailComposeViewController将To,Subject或Body字段设置为第一响应者?
换句话说,行为将是:用户按下呈现邮件视图的按钮(presentModalViewController)。显示邮件视图时,光标将放在其中一个字段中,键盘将打开。
我注意到MFMailComposeViewController文档说:
“重要提示:邮件撰写界面本身不可自定义,不得由您的应用程序修改。此外,在显示界面后,您的应用程序不允许对电子邮件内容进行进一步更改。仍然可以使用界面编辑内容,但忽略程序化更改。因此,您必须在呈现界面之前设置内容字段的值。“
但是,我不关心自定义界面。我只想设置firstResponder。有什么想法吗?
答案 0 :(得分: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];
答案 1 :(得分:4)
在iOS 6中,不再可以在任何文本字段AFAICT上设置第一响应者。导航视图层次结构最终会显示UIRemoteView,并且此处的子视图将被混淆掉。
答案 2 :(得分:1)
您可以尝试在控制器本身上调用becomeFirstResponder。如果这不起作用,您可以尝试在调试器中获取邮件撰写视图的子视图列表,直到找到熟悉的文本字段或文本视图,然后您可以专门编写代码以在代码中设置响应者状态,这可能看起来像这(我不知道这是否有效,但这是一个例子):
[[[[mailcomposer.view.subviews objectAtIndex:3] subviews] objectAtIndex:2] becomeFirstResponder]
答案 3 :(得分: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.
}
}