ios - 首先显示 UIAlertController,然后显示 MFMailComposeViewController。仅在第一次工作

标签 ios objective-c uialertcontroller mfmailcomposeviewcontroller

我有 UIAlertController。单击确定,显示 MFMailComposeViewController。我通过点击电子邮件撰写屏幕中的取消按钮关闭了 MFMailComposeViewControllerMFMailComposeViewController 的委托(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/

相关文章:

ios - ASIHTTPRequest加载数据时如何使用UIActivityIndi​​catorView?

iphone - iOS 复制目录包括子目录

iphone - 如何在表格 View 上方创建一个箭头

swift - UIAlertController 延迟显示

objective-c - 如何检查方法 touchesBegan :withEvent: 中的特定事件

objective-c - 如何向 Xcode 添加 #ifdef DEBUG?

objective-c - objective-c ,NSMutableArray : indexOfObject

ios - 通知消息/警报消息和 UICollectionView :

ios - UIAlertController 的按钮在单击时更改其颜色。怎么解决? - swift

ios - 如何使 iOS 9 的 UIPopoverPresentationController 半透明