2015-10-05 64 views
0

我想从集合视图单元格控制器发送邮件,但出现错误。下面是完整的代码:从CollectionViewCell.swift发送邮件

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(["[email protected]"]) 
     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

请参阅http://stackoverflow.com/questions/32829035/open-camera-from-uitableviewcell-ios了解类似的问题和相关解决方案。 – rmaddy

+0

@rmaddy - 是的,使用'self.presentViewController(mailComposeViewController,animated:true,completion:nil)'。我得到同样的错误.. – IdaEmilie

+0

'self'需要成为视图控制器。 – rmaddy

回答

0

您可以使用完成块或委托来通知视图控制器,如下所示。您可以在cellForItemAtIndexPath的单元上设置delegatenotifyViewController。通过这种方式,您可以告诉每个单元您要处理哪个控制器以呈现邮件控制器,并且控制器可以决定如何执行此操作。

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() 
     } 
    } 

    //... 
} 
+0

按下'buttonClick'时没有任何事情发生,只有println函数。 – IdaEmilie

+0

你是否将'delegate'或'notifyViewController'设置为任何东西?视图控制器是否设置了这个代码,然后执行'presentViewController(...)'?这只是单元和视图控制器之间的通信手段;对于呈现视图控制器来说不是一个神奇的答案 – keithbhunter

+0

它应该是正确的,我已经设置了两个:http://s3.postimg.org/ohco65az7/Screen_Shot_2015_10_05_at_19_20_43.png和http://s12.postimg.org/b2gtufjdp/Scr​​een_Shot_2015_10_05_at_19_21_07.png – IdaEmilie