Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法从单独的ViewController中关闭MFMailComposeViewController_Ios_Swift_Xcode_Email - Fatal编程技术网

Ios 无法从单独的ViewController中关闭MFMailComposeViewController

Ios 无法从单独的ViewController中关闭MFMailComposeViewController,ios,swift,xcode,email,Ios,Swift,Xcode,Email,我有一个DataHandler对象,它在实例化时在不同的ViewController上显示MFMailComposeViewController 例如,在一个类中,我有一个iAction,它通过电子邮件发送数据并调用DataHandler来执行此操作: @IBAction func exportData(_ sender: Any) { let dh = DataHandler() dh.sendEmail(vc: self) } DataHandler然后执行以下操作: fu

我有一个DataHandler对象,它在实例化时在不同的ViewController上显示MFMailComposeViewController

例如,在一个类中,我有一个iAction,它通过电子邮件发送数据并调用DataHandler来执行此操作:

@IBAction func exportData(_ sender: Any) {
    let dh = DataHandler()
    dh.sendEmail(vc: self)
}
DataHandler然后执行以下操作:

func configureEmail() -> MFMailComposeViewController{
    let mailComposerVC = MFMailComposeViewController()
    let dataHandler = DataHandler()
    let data = dataHandler.getData(fileName: file!)
    mailComposerVC.mailComposeDelegate = self
    mailComposerVC.setToRecipients([emailAddr!])
    mailComposerVC.setSubject("blah!")
    mailComposerVC.addAttachmentData(data as Data, mimeType: "text/csv", fileName: "file.csv")
    return mailComposerVC
}

func sendEmail(vc: UIViewController){
    if MFMailComposeViewController.canSendMail() {
        let emailClient = configureEmail()
        vc.show(emailClient, sender: vc)
    }
    else{
        print("Unable to send emails")
    }
}
我可以发送电子邮件,但无法在发送视图控制器后将其关闭。我已经在DataHandler中插入了这个

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

    // Dismiss the mail compose view controller.
    controller.dismiss(animated: true, completion: nil)
}
我已经在互联网上搜寻解决方案,现在束手无策。请帮忙!:)

编辑1:
已将控制器。解除(动画:true,完成:nil)更改为self。解除(动画:true,完成:nil)

您需要设置代理:

mailComposerVC.mailComposertDelegate = self

已在IBAction的范围之外定义。因此,解除MFMailComposeViewController所需的DataHandler对象不在解除函数的同一作用域中。解决方案是将DataHandler对象声明为调用DataHandler函数的类的变量。

您好,我想我这样做了:mailComposerVC.mailComposeDelegate=self正在调用的委托方法是什么?MailComposer TVC应该被解雇,但不是母公司vc。你是说解雇邮件vc的人吗?是的;mailComposeController没有完成,老实说,我不确定。我最近才开始学习Swift。。我不知道它是如何被调用的。首先:你必须出示这个控制器。
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)