按下“取消”或“发送”按钮后,无法关闭MFMailComposeViewController。我在类里面添加了MFMailComposeViewControllerDelegate,但是仍然无法正常工作吗?

这是我的代码:

func sendEmail() {
    let MailVC = MFMailComposeViewController()
    MailVC.mailComposeDelegate = self
    MailVC.setToRecipients(["\(emailLabel?.text)"])
    MailVC.setSubject("Set your subject here!")
    MailVC.setMessageBody("Hello this is my message body!", isHTML: false)
    // Present the view controller modally.
    self.present(MailVC, animated: true, completion: nil)
}

func mailComposeController(controller: MFMailComposeViewController,
                           didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)
}

最佳答案

委托(delegate)方法签名错误。您缺少_参数之前的controller。试试这个。

public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)
}

并确保这一点。
class ViewController: UIViewController ,MFMailComposeViewControllerDelegate

10-08 12:35