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

相关问题