我创建了一个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