objective-c - Swift 中的 MFMailComposeViewController 不会关闭

标签 objective-c uiviewcontroller swift mfmailcomposeviewcontroller ios8

这是示例代码:

import UIKit
import MessageUI

class ViewController: UIViewController, MFMailComposeViewControllerDelegate {

@IBAction func showEmail(sender : AnyObject) {
    var emailTitle = "Test Email"
    var messageBody = "This is a test email body"
    var toRecipents = ["a.nakhimov@gmail.com"]
    var mc: MFMailComposeViewController = MFMailComposeViewController()
    mc.mailComposeDelegate = self
    mc.setSubject(emailTitle)
    mc.setMessageBody(messageBody, isHTML: false)
    mc.setToRecipients(toRecipents)

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

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

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

func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
    switch result.value {
    case MFMailComposeResultCancelled.value:
        NSLog("Mail cancelled")
    case MFMailComposeResultSaved.value:
        NSLog("Mail saved")
    case MFMailComposeResultSent.value:
        NSLog("Mail sent")
    case MFMailComposeResultFailed.value:
        NSLog("Mail sent failure: %@", [error.localizedDescription])
    default:
        break
    }
    self.dismissModalViewControllerAnimated(true)
    // self.dismissViewControllerAnimated(true, completion: nil)
}

}

当我按下按钮时,函数 showEmail 被执行并出现发送电子邮件的表单。如果我单击“发送”,那么一切正常 - 发送邮件,然后执行函数 ma​​ilComposeController。 NSLog 显示标签 “邮件已发送” 并重新出现初始屏幕。

如果我在发送邮件的对话框中,点击“取消”按钮,那么对话框不会消失,函数ma​​ilComposeController不起作用,两个按钮- “发送”和“取消”,变成灰色,就这样保留。

怎么了?

最佳答案

您正在尝试关闭 self 而不是 MFMailComposeViewController(这不是 self)。

替换:

self.dismissModalViewControllerAnimated(true)

与:

controller.dismissViewControllerAnimated(true, completion: nil)

在委托(delegate)方法中。

关于objective-c - Swift 中的 MFMailComposeViewController 不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24341881/

相关文章:

ios - 在复杂的 NSAttributedString 中插入字符串

objective-c - 如何自动释放 CGImageRef?

ios - 在没有 segue 的情况下将数据传递给 VC 的最有效和最简洁的方法是什么?

ios - Objective-c 从另一个 View Controller 重新加载 TableView

ios - 类型 'X' 不符合协议(protocol) 'ResponseObjectSerializable'

objective-c - 在 Objective C 中使用带有重载方法的 Swift 类

objective-c - 让我的 NSSecureTextField 触发 NSButton

ios - 在 UIPageViewController 的 ViewController 中更新数据

json - 快速解析响应时出现问题?

ios - 如何使用UIDocumentBrowserViewController在iCloud上重新打开UIDocument