title: Dismissing modal view controller in another modal view controller
body: <p>My iPad app presents a modal view controller from which the user can choose to send an email. My issue is that this opens a new modal view controller <code>(MFMailComposeViewController)</code>, which I then can't seem to dismiss properly. <a href="https://stackoverflow.com/questions/21898920/warning-when-attempting-to-dismiss-view-controller-modal-within-a-modal">This SO thread</a> describes a similar issue, but none of the suggestions in that thread worked for me.</p>
  
  <p>My initial view controller brings up the modal VC using a storyboard segue:</p>
  
  <pre><code>[self performSegueWithIdentifier:@"mySegue" sender:self];
  </code></pre>
  
  <p>The modal view controller calls the following method when a UIButton is pressed to bring up the iOS default mail composer VC:</p>
  
  <pre><code>- (IBAction)sendWithMail
  {
      MFMailComposeViewController *composeMail = [[MFMailComposeViewController alloc] init];
  
      composeMail.mailComposeDelegate = self.presentingController;
  
      [self presentViewController:composeMail animated:YES completion:NULL];
  }
  </code></pre>
  
  <p>Here, <code>self.presentingController</code> is a reference to the initial view controller, which conforms to the <code>&lt;MFMailComposeViewControllerDelegate&gt;</code> protocol. When the user has finished writing the email, the delegate method in my initial VC is called. Now I need to close the mail composer view controller and/or my own modal view controller, depending on the result. So I wrote a method in the initial VC like this:</p>
  
  <pre><code>- (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;
          }
      }
  }
  </code></pre>
  
  <p>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 <code>(lldb)</code> and Xcode highlights the return line of main.m in green with <code>Thread 1: EXC_BAD_ACCESS (code=1, address=...)</code>.</p>
  
  <p>If I don't try to close the mail composer, and just call <code>[self dismissViewControllerAnimated:YES completion:^{}];</code> 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.</p>
  
  <p>I've also tried <code>self.presentingViewController</code> instead of just <code>self</code> in the dismiss calls, but that doesn't help.</p>
  
  <p>I feel like I'm missing something fundamental about how view controllers interact, but I can't figure it out. Any help is appreciated!</p>
  
  <p>iOS 7 + Xcode 5</p>