我創建了一個ContactUsViewController
。UIAlertController不會顯示 - 在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!
但我真的不知道這是什麼話,或者更重要的是,如何解決它。
我的問題是:
- 上午我就在說我不能使用
UIAlertView
? - 我從郵件應用程序返回後,如何解決此問題並使
UIAlerController
顯示?
在此先感謝。
這個工作太棒了!非常感謝! – Nick89