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

标签 ios swift uialertview mfmailcomposeviewcontroller

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

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

无法将“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 用法。

关于ios - 无法将类型 'UIAlertControllerStyle.Type' 的值转换为预期的参数类型 'UIAlertControllerStyle',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40835383/

相关文章:

iOS NSURLConnection _resourceLoadLoop 崩溃

ios - 将 IBOutlet/对象传递到可重复动画的通用函数中

ios - 更新 UIMenuController 中的菜单项

ios - JSONSerialization 和 EXC_BAD_ACCESS

iphone - UITableViewCell 中的 MKMapView

iphone - iOS:viewDidLoad 中的 View 高度设置不正确

ios - 在 Swift 项目中集成 Unity

iphone - 隐藏 UIAlertView

ios - 如何使用两个#imports?

ios - 具有多个零时显示警报消息