0

我有UIAlertController。點擊確定,顯示MFMailComposeViewController。點擊電子郵件撰寫屏幕上的取消按鈕,我解僱了MFMailComposeViewControllerMFMailComposeViewController的委託方法在解散時被正確調用。 MFMailComposeViewController成功解散。緊接着,如果我再次嘗試相同的功能(流程)。我沒有得到警覺,而是越來越低於錯誤。可能是什麼原因?我嘗試了大部分可用的解決方案。仍然遇到同樣的問題。先顯示UIAlertController,然後顯示MFMailComposeViewController。只有第一次作品

試圖提出< UIAlertController:上< MFMailComposeViewController 0x13890beb0>:0x1371ef000>誰的觀點是不是在窗口層次**

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

更新與相關的代碼你的問題。 – rmaddy

+1

你可以請張貼一些代碼嗎?並決定您是使用Swift還是Objective-C – FelixSFD

+1

http://stackoverflow.com/a/25864182/988769 – Kreiri

回答

0

很難說什麼也正是導致問題。我發現了一些可能的原因,希望能解決其中的一個問題,最終將解決您的問題。

您應該在呈現的視圖控制器上不要打電話給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在那裏無法正常工作。嘗試在真實設備上運行,也許,崩潰會神奇消失。

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]; 
+0

以上代碼行是按鈕操作的一部分。我的要求是,在顯示電子郵件作曲者之前顯示警報。 – user516542