5

我創建了一個ContactUsViewControllerUIAlertController不會顯示 - 在Swift中

在此控制器中,用戶將從pickerView中選擇一個選項,然後在textView中輸入消息並按發送電子郵件按鈕。

當他們按下按鈕時,它會創建一個MFMailComposeViewController,以便他們發送電子郵件。現在,當電子郵件被髮送,保存,取消或失敗時,MFMailComposeViewController會在我的應用程序中關閉。我想要一個提醒,然後讓他們知道發生了什麼。我本來已經設置此使用UIAlertView中,並放置在fun mailComposeController功能的代碼,見下圖:

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { 

    switch result.rawValue { 

    case MFMailComposeResultCancelled.rawValue: 
     NSLog("Email cancelled") 
    case MFMailComposeResultSaved.rawValue: 
     NSLog("Email saved") 
     let sendMailErrorAlert = UIAlertView(title: "Email saved", message: "Your email has been saved in Mail.", delegate: self, cancelButtonTitle: "OK") 
     sendMailErrorAlert.show() 
    case MFMailComposeResultSent.rawValue: 
     NSLog("Email sent") 

     let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert) 
     let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil) 
     alertController.addAction(okButton) 
     presentViewController(alertController, animated: true, completion: nil) 

    case MFMailComposeResultFailed.rawValue: 
     NSLog("Email failed: %@", [error!.localizedDescription]) 
     let sendMailErrorAlert = UIAlertView(title: "Oops!", message: "Looks like something went wrong, and the email couldn't send. Please try again later.", delegate: self, cancelButtonTitle: "OK") 
     sendMailErrorAlert.show() 
    default: 
     break 

    } 

正如你所看到的,我已經使用了UIAlertView的電子郵件保存和電子郵件失敗。 這工作絕對好,並按預期顯示我的警報。

我最近讀到UIAlertView從iOS 8開始折舊,我們現在應該使用UIAlertController。因此,我嘗試使用UIAlertController創建相同的東西,您可以在Email Sent部分查看。但是,這似乎不起作用,它只是不顯示任何警報。它打印此錯誤到日誌:

Warning: Attempt to present <UIAlertController: 0x126c50d30> on <XXXX.ContactUsViewController: 0x1269cda70> whose view is not in the window hierarchy! 

但我真的不知道這是什麼話,或者更重要的是,如何解決它。

我的問題是:

  1. 上午我就在說我不能使用UIAlertView
  2. 我從郵件應用程序返回後,如何解決此問題並使UIAlerController顯示?

在此先感謝。

回答

5

的問題是,你的時間調用此函數如下:

present(alertController, animated: true, completion: nil) 

郵件視圖控制器仍然可見。您必須確保alertController顯示在窗口層次結構的頂部。在你的例子中,你有以下窗口層次結構:

+---------------------------+ 
| MailComposeViewController | 
+---------------------------+ 
      || 
+---------------------------+ 
| ContactUsViewController | 
+---------------------------+ 

你應該做的就是先關閉電子郵件視圖控制器。完成後,顯示警報視圖控制器。

let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert) 
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil) 
alertController.addAction(okButton) 
controller.dismissViewControllerAnimated(true){() -> Void in 
    self.present(alertController, animated: true, completion: nil) 
} 

另外,您還可以在MailComposeViewController的頂部展示您的alertController,像這樣:

let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert) 
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil) 
alertController.addAction(okButton) 
controller.present(alertController, animated: true, completion: nil) 
+1

這個工作太棒了!非常感謝! – Nick89

相關問題