Swift 4-发送电子邮件后将Segue绑定到新的ViewController

时间:2018-07-03 00:24:09

标签: segue viewcontroller mfmailcomposeviewcontroller

在我的应用中,我有一个MFMailComposeViewController。当MFMailComposeViewController自我呈现时。当发送被点击时,我需要将其segue到新的viewController。当点击“发送”时,会发生什么。出现新的viewcontroller,然后重新出现MFMailComposeViewController。有人对此有解决方案吗?我需要做的就是在点击“发送”后将其绑定到新的viewController。 谢谢。

导入UIKit 导入MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func send(_ sender: Any) {
    let sendMailVC = configureMailController()
    if MFMailComposeViewController.canSendMail(){
        self.present(sendMailVC, animated: true, completion: nil)
    }
    else{
        showMailError()
    }
}

func configureMailController() -> MFMailComposeViewController{
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self
    mailComposerVC.setToRecipients(["clpowerpalletchecklist@gmail.com", "abeangoalie39@gmail.com"])
    mailComposerVC.setSubject("ForkLift #PP1 Issue")
    mailComposerVC.setMessageBody("Hi Mom", isHTML: false)

    return mailComposerVC
}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    switch result {
    case .cancelled:
        print("canceled")
        break
    case .failed:
        print("big nope")
        break
    case .saved:
        print("saved")
        break
    case .sent:
        self.performSegue(withIdentifier: "by", sender: nil)
        break
    }
    controller.dismiss(animated: true, completion: nil)
}

func showMailError(){
    let sendMailErrorAlert =  UIAlertController (title:"Could not send Email to the following recipient", message: "Please notify your supervicer of the issue.", preferredStyle: .alert)
    let ok:UIAlertAction = UIAlertAction(title: "OK", style: .default){
        (_:UIAlertAction) in
        //self.performSegue(withIdentifier: "emailDone", sender: nil)
    }
    sendMailErrorAlert.addAction(ok)
    self.present(sendMailErrorAlert, animated: true, completion: nil)
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

1 个答案:

答案 0 :(得分:0)

尝试使用带有dismiss(动画:true,完成:nil)的弹出消息,而不是 self.performSegue(withIdentifier:“ by”,发送者:nil)