[英]Showing UIAlertController first then MFMailComposeViewController. Works only first time
I have UIAlertController
. 我有
UIAlertController
。 On click of OK, presenting MFMailComposeViewController
. 单击确定,显示
MFMailComposeViewController
。 I dismiss the MFMailComposeViewController
by tapping on cancel button in email compose screen. 我通过点击电子邮件撰写屏幕中的取消按钮来关闭
MFMailComposeViewController
。 MFMailComposeViewController
's delegate methods are called properly while dismissing. 关闭时,会正确调用
MFMailComposeViewController
的委托方法。 MFMailComposeViewController
dismisses successfully. MFMailComposeViewController
成功关闭。 Immediately after that if I try the same feature(flow) again. 之后,如果我立即再次尝试相同的功能(流程)。 I am not getting alert, rather getting below error.
我没有收到警报,而是出现了错误。 What could be the reason?
可能是什么原因? I tried most of the solution available in stackoverflow.
我尝试了stackoverflow中可用的大多数解决方案。 still getting the same issue.
仍然遇到同样的问题。
Attempt to present <UIAlertController: 0x13890beb0> on <MFMailComposeViewController: 0x1371ef000> whose view is not in the window hierarchy!**
尝试在<MFMailComposeViewController:0x1371ef000>上显示<UIAlertController:0x13890beb0>,其视图不在窗口层次结构中!**
I am using self presentViewController
to present alertcontroller and MFMailComposeViewController
. 我正在使用
self presentViewController
来呈现alertcontroller和MFMailComposeViewController
。
sample code is here : 示例代码在这里:
- (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];
Please use below code to present mail 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];
Hard to say what's exactly causing a problem. 很难说到底是什么引起了问题。 I've found a few possible reasons, hopefully, fixing one of them will end up as a solution to your problem.
我发现了一些可能的原因,希望修复其中一个最终可以解决您的问题。
You should call dismissViewControllerAnimated:
on the presenting view controller not the presented one. 您应该在呈现的视图控制器上而不是呈现的视图控制器上调用
dismissViewControllerAnimated:
Even though it usually works, in your case it could brake something. 即使它通常可以工作,但在您的情况下,它还是可以刹车。 You have 3 places where you do it wrong.
您在3个地方做错了地方。 This is one of them:
这是其中之一:
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil]; // `controller` is replaces with `self`
}
Also you shouldn't present mailComposerVC
before alertController
is dismissed. 另外,在
alertController
被关闭之前,您不应该显示mailComposerVC
。 You may use the completion block for this. 您可以为此使用完成块。
Do you test on the simulator? 您是否在模拟器上进行测试?
MFMailComposeViewController
doesn't work properly there. MFMailComposeViewController
在此处无法正常工作。 Try to run on a real device, maybe, the crash will magically disappear. 尝试在真实设备上运行,也许崩溃会神奇地消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.