我有 UIAlertController
。单击确定,显示 MFMailComposeViewController
。我通过点击电子邮件撰写屏幕中的取消按钮关闭了 MFMailComposeViewController
。 MFMailComposeViewController
的委托(delegate)方法在关闭时被正确调用。 MFMailComposeViewController
成功关闭。紧接着,如果我再次尝试相同的功能(流程)。我没有得到警报,而是低于错误。可能是什么原因?我尝试了 stackoverflow 中可用的大部分解决方案。仍然遇到同样的问题。
Attempt to present <UIAlertController: 0x13890beb0> on <MFMailComposeViewController: 0x1371ef000> whose view is not in the window hierarchy!**
我正在使用 self presentViewController
来呈现 alertcontroller 和 MFMailComposeViewController
。
示例代码在这里:
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error{
[controller dismissViewControllerAnimated:YES completion: nil];
}
UIAlertController * alertController= [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alertController dismissViewControllerAnimated:YES completion:nil];
MFMailComposeViewController *mailComposerVC = [MFMailComposeViewController new];
mailComposerVC.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[self presentViewController:(MFMailComposeViewController*)mailComposerVC animated: true completion: nil];
}
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:ok];
[alertController addAction:cancel];
[self presentViewController:alertController animated:false completion:nil];
最佳答案
请使用以下代码来显示邮件 Controller :
UIAlertController * alertController= [UIAlertController alertControllerWithTitle:@"Title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
mailComposerVC.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:mailComposerVC
animated:YES
completion:nil];
}
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:ok];
[alertController addAction:cancel];
[self presentViewController:alertController animated:false completion:nil];
关于ios - 首先显示 UIAlertController,然后显示 MFMailComposeViewController。仅在第一次工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40245387/