【问题标题】:UITextField fails to show keyboard after MFMailComposeViewController is dismissed关闭 MFMailComposeViewController 后 UITextField 无法显示键盘
【发布时间】:2013-03-14 03:12:37
【问题描述】:

我遇到了一个问题,即在显示 UITextField 时键盘正确显示,但在 MFMailComposeViewController 出现并关闭后无法显示。光标出现在文本字段中,但没有键盘。

基本上,当用户点击“创建预设”按钮时,UIView 会添加到包含 UITextField 的当前视图中:

[self.view addSubview:createPresetView];
[presetNameField becomeFirstResponder];

这行得通。用户可以添加多个新预设,每次添加视图时,文本字段成为第一响应者,并出现键盘。为了排除 presetNameField 不在视图层次结构中的问题,我还尝试在 1 秒延迟后执行 becomeFirstResponder - 结果相同。

接下来,可以使用标准的 MFMailComposeViewController 通过电子邮件共享预设:

MFMailComposeViewController *mailView = [[MFMailComposeViewController alloc] init];
[mailView setMailComposeDelegate:appController];
[mailView setSubject:@"New preset"];
[mailView setMessageBody:@"\n\nMail from app" isHTML:NO];
[appController presentModalViewController:mailView animated:YES];

据我所知,邮件撰写视图正确关闭:

[controller.view endEditing:YES]; // I added this as a test to ensure the mail view resigns first responder, no difference.
[self dismissModalViewControllerAnimated:YES];
[controller release];
controller = nil;

但现在“创建预设”按钮添加了带有文本字段的视图,将文本字段设置为第一响应者,但从未出现任何键盘。

我已经检查了当我在文本字段上成为 FirstResponder 时发生了什么。文本字段本身是有效的,并且返回 YES 给 canBecomeFirstResponder。

我发现的唯一潜在线索是 [presetNameField becomeFirstResponder] 总是 返回 NO(甚至在显示邮件视图之前)。

有什么建议吗?

【问题讨论】:

  • 你找到解决办法了吗?

标签: iphone keyboard uitextfield mfmailcomposeviewcontroller


【解决方案1】:

MFMailComposeViewController 像您期望的那样接受第一响应者。没有将 firstResponder 返回到您的文本字段的内置机制。但是您可以在viewWillAppear: 上进行操作。

你甚至可以这样做,只有当像这样从呈现的视图控制器(如 MFMailCompose)返回时:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // this is optional, if you care about context of presentation
    if (!self.isBeingPresented) {  // if we're not on our way up the stack of vcs
        [self.presetNameField becomeFirstResponder];
    }
}

【讨论】:

  • 谢谢,但文本字段不需要自动成为第一响应者(仅当用户尝试创建新预设并且应用程序要求输入名称时才会发生)。问题是文本字段在成为第一响应者时不显示键盘(但仅在显示 MailComposeView 之后)。
猜你喜欢
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多