Swift中的MFMailComposeViewController不会被忽略

时间:2014-06-21 13:04:04

标签: 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 功能,并显示发送电子邮件的表单。如果我点击"发送",那么一切正常 - 发送邮件,然后执行函数 mailComposeController 。 NSLog显示标签"邮件发送" ,初始屏幕重新出现。

如果我在发送邮件对话框中,请点击"取消" 按钮,然后对话框不会消失,功能 mailComposeController 不起作用,两个按钮 - "发送"和"取消",变成灰色,因此保持不变。

有什么不对?

3 个答案:

答案 0 :(得分:3)

您试图取消self而不是MFMailComposeViewController(这不是自我)。

替换:

self.dismissModalViewControllerAnimated(true)

使用:

controller.dismissViewControllerAnimated(true, completion: nil)

在委托方法中。

答案 1 :(得分:2)

似乎是iOS 8中的错误。目标-C中也存在同样的问题。

答案 2 :(得分:2)

Bug已经解决。在iOS 8.1+和Xcode 6.1 +

中,一切都按预期工作

以下火灾并且工作正常

extension MainViewController: MFMailComposeViewControllerDelegate {
    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.dismissViewControllerAnimated(true, completion: nil)
    }
}