无法将类型 'UIAlertControllerStyle.Type' 的值转换为预期的参数类型 'UIAlertControllerStyle'

Cannot convert value of type 'UIAlertControllerStyle.Type' to expected argument type 'UIAlertControllerStyle'

我的应用程序上有一个 UIButton,它在点击时显示 UIAlertView。警报视图将包含一个按钮,用于打开 iOS 电子邮件撰写视图。

我的邮件撰写视图运行良好,但是当用户发送电子邮件或点击 "cancel" 时,邮件撰写视图不会消失。我正在使用的代码似乎不起作用,因为我收到此错误:

无法将类型 'UIAlertControllerStyle.Type' 的值转换为预期的参数类型 'UIAlertControllerStyle'

            var alert = UIAlertController(title: "Alert", message: "Your Device cannot send emails", preferredStyle: UIAlertControllerStyle)

这里可能发生了什么?谢谢!

var myMail: MFMailComposeViewController!

@IBAction func helpfeedbackAlert(_ sender: Any) {

    if(MFMailComposeViewController.canSendMail()){
        myMail = MFMailComposeViewController()

        myMail.setSubject("Test")
        myMail.setToRecipients(["test@test.com"])

        self.present(myMail, animated: true, completion: nil)
    }
    else{
        var alert = UIAlertController(title: "Alert", message: "Your Device cannot send emails", preferredStyle: UIAlertControllerStyle)
        self.present(alert, animated: true, completion: nil)

    }

}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.          
}

func mailComposeController(controller: MFMailComposeViewController!, didFinishWith: MFMailComposeResult, error: NSError!){

    switch result.rawValue {

    case MFMailComposeResult.cancelled.rawValue:
        print("Mail cancelled")

    case MFMailComposeResult.sent.rawValue:
        print("Your email has been sent!")

    case MFMailComposeResult.failed.rawValue:
        print("Email has failed to send: %@", [error!.localizedDescription])
    default:
        break

    }

    // Dismiss the mail compose view controller
    controller.dismiss(animated: true, completion: nil)


}

警报问题很简单。您需要指定特定的警报样式 (.alert),但您传递的是枚举的名称,而不是传递特定的值。

邮件编辑器不关闭的问题也很简单。你从来没有设置 mailComposeDelegate 属性.

switch 的问题是您的方法签名有误。必须是:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)

而且不需要所有 rawValue 用法。

var alert = UIAlertController(title: "Alert", message: "Your Device cannot send emails", preferredStyle: .Alert)