【问题标题】:Showing UIAlertController first then MFMailComposeViewController. Works only first time首先显示 UIAlertController,然后显示 MFMailComposeViewController。仅第一次工作
【发布时间】:2016-10-25 16:42:47
【问题描述】:

我有UIAlertController。单击确定后,显示MFMailComposeViewController。我通过点击电子邮件撰写屏幕中的取消按钮来关闭MFMailComposeViewControllerMFMailComposeViewController 的委托方法在解雇时被正确调用。 MFMailComposeViewController 成功关闭。在那之后,如果我再次尝试相同的功能(流程)。我没有得到警报,而是低于错误。可能是什么原因?我尝试了 stackoverflow 中提供的大多数解决方案。仍然遇到同样的问题。

尝试在视图不在窗口层次结构中的 上呈现 !**

我使用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];

【问题讨论】:

标签: ios objective-c uialertcontroller mfmailcomposeviewcontroller


【解决方案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];

【讨论】:

  • 以上代码行是按钮动作的一部分。我的要求是,在显示电子邮件作曲家之前显示警报。
【解决方案2】:

很难说究竟是什么导致了问题。我找到了一些可能的原因,希望修复其中一个最终可以解决您的问题。

您应该在呈现视图控制器上调用dismissViewControllerAnimated: 而不是呈现的视图控制器。即使它通常有效,但在您的情况下,它可能会刹车。你有 3 个地方做错了。这是其中之一:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error
{
    [self dismissViewControllerAnimated:YES completion:nil];  // `controller` is replaces with `self`
}

此外,您不应在 alertController 被解雇之前呈现 mailComposerVC。您可以为此使用完成块。

您是否在模拟器上进行测试? MFMailComposeViewController 在那里无法正常工作。尝试在真机上运行,​​说不定崩溃会神奇消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2022-11-14
    相关资源
    最近更新 更多