ios - MFMailComposeViewController didFinishWithResult 未被调用

标签 ios delegates uipopovercontroller mfmailcomposeviewcontroller dismiss

我正在编写一个具有通用表单的应用程序,该应用程序会根据用户在表单中输入的内容进行调整和更改,然后他们会检查并回答表单中较低的问题,因此我在我的代码中进行了答案检查以完成这项工作,当用户单击提交时,邮件撰写 View Controller 加载正常,然后他们单击发送并发送电子邮件,但未调用 didFinishWithResult 函数,这意味着 Composer 不会关闭。请看下面我的代码:

@IBAction func Submit(sender: AnyObject) {

    if SelectedTalkLabel.text! == "Safe Breaking Ground - Toolbox Talk" && ASwitch.on && BSwitch.on && DSwitch.on && !(CSwitch.on)    ||    SelectedTalkLabel.text! == "Loading & Unloading Vehicles - Toolbox Talk" && ASwitch.on && BSwitch.on && CSwitch.on && !(DSwitch.on)    ||    SelectedTalkLabel.text! == "Orteco - Toolbox Talk" && ASwitch.on && CSwitch.on && !(BSwitch.on) && !(DSwitch.on) {


        let toRecipents = ["example@abc.com"]
        let emailTitle = "New ToolBox Talk Submission received!"
        let messageBody = String(format: "")

        let mc : MFMailComposeViewController = MFMailComposeViewController()

        mc.mailComposeDelegate = self


        mc.setToRecipients(toRecipents)
        mc.setSubject(emailTitle)
        mc.setMessageBody(messageBody, isHTML: false)

        self.presentViewController(mc, animated: true, completion: nil)

}

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {

        switch result.rawValue {

        case MFMailComposeResultCancelled.rawValue:
            print("cancelled")
            self.dismissViewControllerAnimated(true, completion: nil)

        case MFMailComposeResultFailed.rawValue:
            print("failed")
            AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
            self.dismissViewControllerAnimated(true, completion: nil)

        case MFMailComposeResultSaved.rawValue:
            print("saved")
            self.dismissViewControllerAnimated(true, completion: nil)

        case MFMailComposeResultSent.rawValue:
            print("sent")
            self.dismissViewControllerAnimated(true, completion: nil)


        default:
            break

        }

        if SelectedTalkLabel.text! == "Safe Breaking Ground - Toolbox Talk" && !(ASwitch.on) || !(BSwitch.on) || !(DSwitch.on)    ||    SelectedTalkLabel.text! == "Loading & Unloading Vehicles - Toolbox Talk" && !(ASwitch.on) || !(BSwitch.on) || !(CSwitch.on)    ||    SelectedTalkLabel.text! == "Orteco - Toolbox Talk" && !(ASwitch.on) || !(CSwitch.on) {

            let WrongAlert = UIAlertController(title: "Wrong Answer!", message: "You have selected the wrong answers, please go back and try again", preferredStyle: .Alert)

            let WrongDismiss = UIAlertAction(title: "Okay", style: .Cancel, handler: {
                (alert: UIAlertAction!) -> Void in
            })

            WrongAlert.addAction(WrongDismiss)

            self.presentViewController(WrongAlert, animated: true, completion: nil)
    }
}

这类似于我在应用程序中以其他形式使用的代码,完全没有戏剧效果。如果有人可以尝试帮助我解决这个问题,我将不胜感激。干杯。

最佳答案

我遇到了同样的问题,过去 2 天一直在寻找修复程序,然后我自己找到了一个修复程序,你不会相信它有多小。

在我的例子中,我呈现 MFMailComposeViewController 的 View Controller (根据这个问题说“DetailsTableViewController”,其中存在“提交”按钮)已经从其他 View Controller 呈现(比如“BaseViewController”)。

问题出在从 BaseViewController 呈现时“DetailsTableViewController”的“modalPresentationStyle”。

当我将其从“UIModalPresentationFormSheet”更改为“UIModalPresentationPageSheet”(就此而言,除“UIModalPresentationFormSheet”之外的任何内容)问题得到解决,邮件 Controller 委托(delegate)方法开始照常触发。

注意:我已经在“DetailsTableViewController”中调用了以下方法(对于此示例),所以我使用的是哪个“modalPresentationStyle”并不重要。

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 1024, 768);
    self.view.superview.backgroundColor = [UIColor clearColor];
}

希望这也能解决您的问题。

关于ios - MFMailComposeViewController didFinishWithResult 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37721463/

相关文章:

ios - 检查对象是否存在于 NSDictionary 的 NSMutableArray 中

ios - 设计模式 : exposing notifications using a protocol

swift - 在 swift 中设置一个泛型类作为委托(delegate)

iphone - 实时格式化美元金额

ios - 将 NSString 与 NSArray 中的单词进行匹配的最快方法

c# - 返回值未显示

c++ - obj-c 委托(delegate)模式的 C++ 等价物是什么?

ios - 如何从 UIViewcontroller 的弹出窗口中的 UItableview 中填充数据?

ios - swift 3 : popover connection from button inside dynamic uitableviewcell to uiviewcontroller

ios - 如何使用 iOS 状态恢复来恢复 UIPopoverController?