我已经创建了一个
在这个控制器中,用户将从
当他们按下按钮时,它会创建一个
如您所见,我使用了
ContactUsViewController
控制器。在这个控制器中,用户将从
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
来处理“电子邮件已保存”和“电子邮件发送失败”的情况。这个方法非常好用,能够按预期显示提示信息。
最近我看到iOS 8以后UIAlertView
被弃用了,现在我们应该使用UIAlertController
。因此,我尝试使用UIAlertController
来创建同样的提示框,您可以在“电子邮件已发送”部分看到。然而,它似乎不起作用,没有显示任何提示信息。日志中会打印出以下错误信息:Warning: Attempt to present <UIAlertController: 0x126c50d30> on <XXXX.ContactUsViewController: 0x1269cda70> whose view is not in the window hierarchy!
但我并不确定这是什么意思,更重要的是,如何解决它。
我的问题是:
- 我说不能使用
UIAlertView
,这样说对吗? - 我该如何修复它并使
UIAlerController
在从邮件应用程序返回后出现?
提前感谢。