ios - Swift 从滑动菜单中关闭邮件 Controller

标签 ios swift mfmailcomposeviewcontroller

我在使用 MFMailComposer 时遇到了问题。我已经使用示例 Swift Code 遵循了 iOS 开发人员指南.

我还查看了本网站上的以下类似问题:

swift - dismissing mail view controller from Sprite Kit

MFMailComposeViewController in Swift does not dismiss

链接的框架和库:MessagesUI.framework

import MessageUI

class FHCLLeftMenuViewController: FHBaseViewController,UITableViewDelegate,UITableViewDataSource,MFMailComposeViewControllerDelegate 

当我点击应用左侧滑动菜单中的“支持”文本时,“邮件”应用出现,我撰写了一封电子邮件并单击“发送”。该电子邮件出现在我的收件箱中,但 Controller 在发送邮件后并未自动关闭。当我尝试删除草稿并且必须关闭应用程序并重新启动时,也会发生这种情况。

        case kFHCLMenuTagSupport:
        //PT DashBoard

        if !MFMailComposeViewController.canSendMail() {
            print("Mail services are not available")
            return
        }

        let composeVC = MFMailComposeViewController()
        composeVC.mailComposeDelegate = self

        // Configure the fields of the interface.
        composeVC.setToRecipients(["support@example.com"])
        composeVC.setSubject("Client Support")
        composeVC.setMessageBody("Some text here.", isHTML: false)



        // Present the view controller modally.
        self.presentViewController(composeVC, animated: true, completion: nil)

        func mailComposeController(controller: MFMailComposeViewController,
                                   didFinishWithResult result: MFMailComposeResult, error: NSError?) {
            // Check the result or perform other tasks.

            // Dismiss the mail compose view controller.
            self.dismissViewControllerAnimated(true, completion: nil)

        }
        break

我也尝试过这种变体以返回到应用程序的主仪表板,结果相同:

     case kFHCLMenuTagSupport:
        //PT DashBoard
        let storyboard = UIStoryboard(name: "Client", bundle: nil)

        // Create View Controllers
        let mainPage:FHClientDashBoardViewController = storyboard.instantiateViewControllerWithIdentifier("FHClientDashBoardViewController") as! FHClientDashBoardViewController
        let mainPageNav = UINavigationController(rootViewController: mainPage)



        if !MFMailComposeViewController.canSendMail() {
            print("Mail services are not available")
            return
        }

        let composeVC = MFMailComposeViewController()
        composeVC.mailComposeDelegate = self

        // Configure the fields of the interface.
        composeVC.setToRecipients(["support@example.com"])
        composeVC.setSubject("Client Support")
        composeVC.setMessageBody("Some text here.", isHTML: false)



        // Present the view controller modally.
        self.presentViewController(composeVC, animated: true, completion: nil)

        func mailComposeController(controller: MFMailComposeViewController,
                                   didFinishWithResult result: MFMailComposeResult, error: NSError?) {
            // Check the result or perform other tasks.

            // Dismiss the mail compose view controller.
            // self.dismissViewControllerAnimated(true, completion: nil)
            self.mm_drawerController.setCenterViewController(mainPageNav, withCloseAnimation: true, completion: nil)
        }

        break

任何帮助将不胜感激。

最佳答案

dismissViewControllerAnimated(_:completion:) 函数关闭调用该函数的 View Controller 。在这种情况下,由于您在 self 上调用它,它会尝试关闭 FHCLLeftMenuViewController。这可以通过在 MFMailComposeViewController 上调用 dismissViewControllerAnimated(_:completion:) 来轻松解决。

为此,只需将 mailComposeController(controller:didFinishWithResult:error:) 函数替换为以下内容(它在 Apple 自己的文档中也以这种方式出现):

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
    // Check the result or perform other tasks.

    // Dismiss the mail compose view controller.
    controller.dismissViewControllerAnimated(true, completion: nil)
}

关于ios - Swift 从滑动菜单中关闭邮件 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39002848/

相关文章:

ios - SwiftUI:在 TextField 内填充

ios - Theos实例方法调用

php - Swift 3 如何转换此函数以返回 json 变量的内容

ios - 在 Swift 中为 UITextField 创建自定义输入

swift - 类型 '()"不符合协议(protocol) : 'BooleanType'

ios - 从附加到电子邮件的文件名 PDF 的 NSString 中删除一些特殊字符

loops - 如何从响应中断的内部结构中中断外部循环(循环/开关)

ios - 如何快速在另一个 View Controller 中获取 NSuser 默认值

iphone - 如何使用 MFMailComposeViewController 设置收件人

ios - 应用内电子邮件发出嗖嗖声但不发送