UIAlertController将无法显示 - 在Swift中

时间:2016-03-06 15:36:05

标签: ios swift uialertview mfmailcomposeviewcontroller uialertcontroller

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

但我不确定这是说什么,或者更重要的是,如何解决它。

我的问题是:

  1. 我是否正确地说我不能使用UIAlertView
  2. 如何修复此问题并在从Mail应用程序返回后显示UIAlerController
  3. 提前致谢。

4 个答案:

答案 0 :(得分: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)   
}

或者,您也可以在alertController之上展示MailComposeViewController,如下所示:

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 :(得分:0)

Swift 4

不再显示警告消息

在DidLoad

let AlertOnce = UserDefaults.standard
    if(!AlertOnce.bool(forKey: "oneTimeAlert")){


        let messageTitle = "Your selected branch is \(storeIDName)"
        let alertView = UIAlertController(title: messageTitle, message: "", preferredStyle: .actionSheet)
        let DoNotShowAgainAction = UIAlertAction(title: "Do Not Show Again", style: UIAlertActionStyle.default) { (action:UIAlertAction) in

            AlertOnce.set(true , forKey: "oneTimeAlert")
            AlertOnce.synchronize()

        }
        let change = UIAlertAction(title: "Change Branch", style: .default){(action) in
            let myVC = self.storyboard?.instantiateViewController(withIdentifier: "FindRestaurentViewControllerID") as! FindRestaurentViewController
            self.navigationController?.pushViewController(myVC, animated: true)
        }
        alertView.addAction(DoNotShowAgainAction)
        alertView.addAction(change)
        present(alertView, animated: true,completion: nil)
        alertView.setValue(NSAttributedString(string: messageTitle, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 20),NSAttributedStringKey.foregroundColor : UIColor(red: 234/256, green: 104/256, blue: 26/256, alpha: 1)]), forKey: "attributedTitle")
        alertView.view.tintColor = UIColor.black

    }

答案 2 :(得分:0)

我需要一个actionSheet而不是一个警报,并且由于我的特定情况,此处没有解决方案。如果您的UIAlertController没有显示,另一种方法是始终将其置于最顶层窗口(在Swift 4中):

func topmostController() -> UIViewController? {
    if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        return topController
    }
    return nil
}

// Now put up the menu
let menu=UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
menu.addAction(UIAlertAction(title: "Test", style: .default, handler: {_ in self.doTest()} ))
menu.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {_ in self.doCancel()} ))
topmostController()?.present(menu, animated: true, completion: nil)

无论您的视图控制器堆栈如何,都会显示该窗口。

答案 3 :(得分:0)

在DispachQueue.main.async {}中显示警报后,我现在面临着同样的问题,代码工作正常。