在另一个模态视图控制器中关闭模态视图控制器

时间:2014-07-09 06:34:02

标签: ios objective-c uiviewcontroller

我的iPad应用程序提供了一个模态视图控制器,用户可以从中选择发送电子邮件。我的问题是,这会打开一个新的模态视图控制器(MFMailComposeViewController),然后我似乎无法正确解雇。 This SO thread描述了一个类似的问题,但该主题中没有任何建议对我有效。

我的初始视图控制器使用故事板segue显示模态VC:

[self performSegueWithIdentifier:@"mySegue" sender:self];

当按下UIButton以显示iOS默认邮件编辑器VC时,模态视图控制器调用以下方法:

- (IBAction)sendWithMail
{
    MFMailComposeViewController *composeMail = [[MFMailComposeViewController alloc] init];

    composeMail.mailComposeDelegate = self.presentingController;

    [self presentViewController:composeMail animated:YES completion:NULL];
}

这里,self.presentingController是对初始视图控制器的引用,它符合<MFMailComposeViewControllerDelegate>协议。当用户完成电子邮件编写后,将调用初始VC中的委托方法。现在我需要关闭邮件编辑器视图控制器和/或我自己的模态视图控制器,具体取决于结果。所以我在初始VC中编写了一个方法,如下所示:

- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error
{

    [controller dismissViewControllerAnimated:YES completion:NULL];

    switch (result)
    {
        case MFMailComposeResultCancelled:
        {
            break;
        }
        case MFMailComposeResultSaved:
        {
            [self updateStatusMessage:@"Mail saved."];
            break;
        }
        case MFMailComposeResultSent:
        {
            [self updateStatusMessage:@"Mail sent."];
            [self dismissViewControllerAnimated:YES completion:^{}];
            break;
        }
        case MFMailComposeResultFailed:
        {
            [self updateStatusMessage:@"Mail failed."];
            break;
        }
        default:
        {
            break;
        }
    }
}

这个想法是邮件编辑器应该总是关闭,但模态视图控制器应该只在邮件发送成功时关闭。但这不起作用:当邮件发送时,应用程序崩溃。唯一的日志输出是(lldb),Xcode用Thread 1: EXC_BAD_ACCESS (code=1, address=...)突出显示main.m的返回行。

如果我没有尝试关闭邮件编辑器,只要调用[self dismissViewControllerAnimated:YES completion:^{}];而不管结果如何,那么作曲家和我的模态视图控制器都会关闭,没有问题。但这不是我想要的行为。

我还在解雇电话中尝试self.presentingViewController而非self,但这并没有帮助。

我觉得我错过了关于视图控制器如何交互的基本信息,但我无法弄明白。任何帮助表示赞赏!

iOS 7 + Xcode 5

2 个答案:

答案 0 :(得分:3)

以这种方式实现MFMailComposeViewController委托方法。

 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }
    [[controller presentingViewController] dismissViewControllerAnimated:YES completion:^{

        if (result == MFMailComposeResultSent)
        {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
   }];

}

答案 1 :(得分:0)

- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error
{


    switch (result)
    {
        case MFMailComposeResultCancelled:
        {
            break;
        }
        case MFMailComposeResultSaved:
        {
            [self updateStatusMessage:@"Mail saved."];
            break;
        }
        case MFMailComposeResultSent:
        {
            [self updateStatusMessage:@"Mail sent."];
          [self dismissViewControllerAnimated:YES completion:nil];
            break;
        }
        case MFMailComposeResultFailed:
        {
            [self updateStatusMessage:@"Mail failed."];
            break;
        }
        default:
        {
            break;
        }
    }

              [self dismissViewControllerAnimated:YES completion:nil];

}

希望它有所帮助。