简体   繁体   English

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

[英]Dismissing modal view controller in another modal view controller

My iPad app presents a modal view controller from which the user can choose to send an email. 我的iPad应用程序提供了一个模式视图控制器,用户可以从中选择发送电子邮件。 My issue is that this opens a new modal view controller (MFMailComposeViewController) , which I then can't seem to dismiss properly. 我的问题是,这将打开一个新的模式视图控制器(MFMailComposeViewController) ,然后我似乎无法正确关闭它。 This SO thread describes a similar issue, but none of the suggestions in that thread worked for me. 这个SO线程描述了一个类似的问题,但是该线程中的任何建议对我来说都不起作用。

My initial view controller brings up the modal VC using a storyboard segue: 我的初始视图控制器使用情节提要segue来启动模态VC:

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

The modal view controller calls the following method when a UIButton is pressed to bring up the iOS default mail composer VC: 当按下UIButton来调出iOS默认邮件编写器VC时,模式视图控制器将调用以下方法:

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

    composeMail.mailComposeDelegate = self.presentingController;

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

Here, self.presentingController is a reference to the initial view controller, which conforms to the <MFMailComposeViewControllerDelegate> protocol. 在这里, self.presentingController是对初始视图控制器的引用,它符合<MFMailComposeViewControllerDelegate>协议。 When the user has finished writing the email, the delegate method in my initial VC is called. 用户写完电子邮件后,将在我的初始VC中调用委托方法。 Now I need to close the mail composer view controller and/or my own modal view controller, depending on the result. 现在,根据结果,我需要关闭mail composer视图控制器和/或我自己的模式视图控制器。 So I wrote a method in the initial VC like this: 所以我在最初的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;
        }
    }
}

The idea is that the mail composer should always close, but the modal view controller should close only if the mail was sent successfully. 这个想法是,邮件编写器应该始终关闭,但是模态视图控制器应该仅在邮件成功发送后关闭。 But this doesn't work: when the mail is sent, the app crashes. 但这是行不通的:发送邮件后,应用程序崩溃了。 The only log output is (lldb) and Xcode highlights the return line of main.m in green with Thread 1: EXC_BAD_ACCESS (code=1, address=...) . 唯一的日志输出是(lldb) ,Xcode用Thread 1: EXC_BAD_ACCESS (code=1, address=...)以绿色突出显示main.m的返回行。

If I don't try to close the mail composer, and just call [self dismissViewControllerAnimated:YES completion:^{}]; 如果我不尝试关闭邮件[self dismissViewControllerAnimated:YES completion:^{}]; ,只需调用[self dismissViewControllerAnimated:YES completion:^{}]; regardless of the result, then both the composer and my modal view controller close and there is no problem. 不管结果如何,然后作曲家和我的模态视图控制器都将关闭并且没有问题。 But this is not the behaviour I want. 但这不是我想要的行为。

I've also tried self.presentingViewController instead of just self in the dismiss calls, but that doesn't help. 我还尝试了self.presentingViewController而不是在dismiss调用中仅使用self ,但这无济于事。

I feel like I'm missing something fundamental about how view controllers interact, but I can't figure it out. 我觉得我缺少关于视图控制器交互方式的一些基本知识,但我无法弄清楚。 Any help is appreciated! 任何帮助表示赞赏!

iOS 7 + Xcode 5 iOS 7 + Xcode 5

Just implement MFMailComposeViewController delegate method this way. 只需以这种方式实现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];
        }
   }];

} }

- (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];

}

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM