MFMailComposeViewControllerDelegate not being called

我只是一个虾纸丫 提交于 2019-12-02 01:42:43

问题


I realize this question has been inexactly asked around, but I haven't been able to find an answer to my problem.

I have a UITableViewController with static cells. One of these cells is meant to open a Mail Composer view, and dismiss it through the delegate after the user sends or cancels the email. My problem is that the delegate method is not being called. Here is my code:

class SideMenuTableViewController: UITableViewController, MFMailComposeViewControllerDelegate, UINavigationControllerDelegate {

 override func viewDidLoad() {
        super.viewDidLoad()

        mailCVP.delegate = self
mailCVP = configureMailComposeVC()
            if MFMailComposeViewController.canSendMail() {
                self.presentViewController(mailCVP, animated: true, completion: nil)
            } else { //..// }
}

func configureMailComposeVC() -> MFMailComposeViewController {
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.setToRecipients(["momentosdetora@gmail.com"])
            mailComposerVC.setSubject("Contacto Momentos de Tora")

        return mailComposerVC
    }

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }

Can anybody spot anything I might be doing wrong?

Thanks.


回答1:


MFMailComposeViewController is a subclass of UINavigationController, which already has a delegate property to handle navigation changes.

MFMailComposeViewController has another property called mailComposeDelegate, which is the property you are looking for.

Also, you should create the controller before setting the delegate.




回答2:


Make sure you use

controller.mailComposeDelegate = self

Not this one

controller.delegate = self



回答3:


mailCVP.delegate = self
mailCVP = configureMailComposeVC()

This code sets the delegate but then creates a new instance, which doesn't have a delegate...

Note that there is also no point in creating the VC instance if MFMailComposeViewController.canSendMail returns false.




回答4:


First of all use

mailCVP.mailComposeDelegate = self

instead of

mailCVP.delegate = self

Moreover, in case of Swift 3, delegate method is somehow updated which is:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?){
    controller.dismiss(animated: true, completion: nil)
}


来源:https://stackoverflow.com/questions/37570786/mfmailcomposeviewcontrollerdelegate-not-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!