从CollectionViewCell.swift发送邮件

时间:2015-10-05 16:55:09

标签: ios swift uicollectionview

我正在尝试从集合视图单元控制器发送邮件,但是我收到错误。这是完整的代码:

import Foundation
import UIKit
import MessageUI

import Parse
import Bolts

class CollectionViewCell: UICollectionViewCell, MFMailComposeViewControllerDelegate {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var imageText: UILabel!
    @IBOutlet weak var uploadedTimeLabel: UILabel!
    @IBOutlet weak var currentUserLabel: UILabel!
    @IBOutlet weak var flagContentButton: UIButton!

    var deviceID = [String]()

    var parseObject:PFObject?

    @IBAction func buttonClick(sender: AnyObject) {
        println(deviceID)

        let mailComposeViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            self.presentViewController(mailComposeViewController, animated: true, completion: nil)
        } else {
            self.showSendMailErrorAlert()
        }
    }

    func configuredMailComposeViewController() -> MFMailComposeViewController {
        let mailComposerVC = MFMailComposeViewController()
        mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property

        mailComposerVC.setToRecipients(["test@email.com"])
        mailComposerVC.setSubject("Test subject")
        mailComposerVC.setMessageBody("Test mail!", isHTML: false)

        return mailComposerVC
    }

    func showSendMailErrorAlert() {
        let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
        sendMailErrorAlert.show()
    }

    // MARK: MFMailComposeViewControllerDelegate

    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
        controller.dismissViewControllerAnimated(true, completion: nil)

    }
}

错误行:

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

错误代码:

  

' CollectionViewCell'没有名为的成员   ' presentViewController'

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您可以使用完成块或委托来通知视图控制器,如下所示。您可以在delegate的单元格中设置notifyViewControllercellForItemAtIndexPath。这样,您可以告诉每个单元您要处理哪个控制器呈现邮件控制器,控制器可以决定如何操作。

protocol CollectionViewCellDelegate {
    func notifyViewController(mailViewController: MFMailComposeViewController)
}

class CollectionViewCell: ... {
    //...

    var delegate: CollectionViewCellDelegate?

    // you could use this completion block property in place of a delegate
    var notifyViewController: ((MFMailComposeViewController) -> Void)?

    //...

    @IBAction func buttonClick(sender: AnyObject) {
        println(deviceID)

        let mailComposeViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            delegate?.notifyViewController(mailComposeViewController)
            // OR
            notifyViewController?(mailComposeViewController)
        } else {
            self.showSendMailErrorAlert()
        }
    }

    //...
}