title: whose view is not in the window hierarchy - email form
body: <p>I'm calling a method from viewcontroller in an appdelegate. When I was testing functionality I just used NSLog message which works fine (so the connection between viewcontroller and appdelegate is OK). The problem appears once I add a email form into this method. The message I receive is: </p>
  
  <pre><code>Warning: Attempt to present &lt;MFMailComposeViewController: 0x1fdc3990&gt; on &lt;ViewController: 0x1fd9e3b0&gt; whose view is not in the window hierarchy!
  </code></pre>
  
  <p>Anyone who know what to do? I know there are lot of topics with 'whose view is not in the window hierarchy' issue but none of them helped me.</p>
  
  <p>ViewController.m </p>
  
  <pre><code>...
  -(void)mail{
      NSLog(@"blablabla");
      if ([MFMailComposeViewController canSendMail]) {
  
          MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
          mail.mailComposeDelegate = self;
  
          [mail setSubject:@"Hello and welcome!"];
  
          NSArray *toRecipients = [NSArray arrayWithObject:@"tomas.javnicky@gmail.com"];
          [mail setToRecipients:toRecipients];
          [mail setCcRecipients:toRecipients];
  
          NSString *emailBody = @"Hey all!";
          [mail setMessageBody:emailBody isHTML:NO];
  
          mail.modalPresentationStyle = UIModalPresentationPageSheet;
          [self presentViewController:mail animated:YES completion:nil];
  
  
      } else {
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                          message:@"E-mail is not supported on your device"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
          [alert show];
      }
  }
  
  -(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
  
      switch (result) {
          case MFMailComposeResultCancelled:
              break;
          case MFMailComposeResultSaved:
              NSLog(@"mail saved");
              break;
          case MFMailComposeResultSent:
              NSLog(@"mail sent");
              break;
          case MFMailComposeResultFailed:
              NSLog(@"mail failed");
              break;
      }
      [self dismissViewControllerAnimated:YES
                               completion:nil];
  }
  ...
  </code></pre>
  
  <p>Appdelegate.m</p>
  
  <pre><code>...
  -(void)something {
       ViewController * vc = [[ViewController alloc] init];
       [vc mail];
  }
  ...
  </code></pre>
  
  <p>This is what solved my problem:</p>
  
  <pre><code>- (void)something {
      ViewController *rootViewController = (ViewController*)self.window.rootViewController;
      [rootViewController mail];
  }
  </code></pre>
  
  <p>Also check the answer by rmaddy for more info.</p>