我有UIAlertController
。点击OK,显示MFMailComposeViewController
。我通过点击电子邮件撰写屏幕中的取消按钮来关闭MFMailComposeViewController
。在解雇时,MFMailComposeViewController
的委托方法被正确调用。 MFMailComposeViewController
成功解雇。之后,如果我再次尝试相同的功能(流程)。我没有得到警觉,而是低于错误。可能是什么原因?我尝试了stackoverflow中提供的大多数解决方案。仍然遇到同样的问题。
尝试出现< UIAlertController:0x13890beb0> on< MFMailComposeViewController:0x1371ef000>其视图不在窗口层次结构中!**
我正在使用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];
答案 0 :(得分:1)
请使用以下代码呈现邮件控制器:
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];
答案 1 :(得分:0)
很难说究竟是什么造成了问题。我发现了一些可能的原因,希望修复其中一个最终可以解决您的问题。
您应该在呈现视图控制器上调用dismissViewControllerAnimated:
而不是呈现的视图控制器。即使它通常有效,但在你的情况下,它可以制动一些东西。你有3个地方你做错了。这是其中之一:
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil]; // `controller` is replaces with `self`
}
在mailComposerVC
被解雇之前,你不应该出现alertController
。您可以使用完成块。
你在模拟器上测试吗? MFMailComposeViewController
在那里工作不正常。尝试在真实设备上运行,也许,崩溃会神奇地消失。