gpt4 book ai didi

ios - 尝试从应用程序快速发送电子邮件

转载 作者:行者123 更新时间:2023-12-01 21:45:15 25 4
gpt4 key购买 nike

我正在尝试在 myViewController 上显示电子邮件弹出窗口,但出现错误

使用未解析的标识符“存在”

在线上

现在( Composer ,动画:真)

值得注意的是,该按钮位于 collectionView 单元格中。我将如何解决此错误,以便当我按下按钮时,电子邮件概述将显示在屏幕上?

这是我的代码。

import MessageUI

class MessagesViewCell: UICollectionViewCell, MFMailComposeViewControllerDelegate {

@IBOutlet weak var textLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBAction func emailButtonTapped(_ sender: Any) {
showMailComposer()
}

func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else {
return
}

let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["email"])
composer.setSubject("")
composer.setMessageBody("", isHTML: false)
composer.present(composer, animated: true)
present(composer, animated: true)
}
}

extension MessagesViewController: MFMailComposeViewControllerDelegate {

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
controller.dismiss(animated: true)
}
switch result {
case .cancelled:
print("Cancelled")
case .failed:
print("Failed to send")
case .saved:
print("Saved")
case .sent:
print("Email Sent")
default:
break
}
controller.dismiss(animated: true)
}
}

最佳答案

解决此问题的最简单方法是传递 UIViewController 的弱引用。到UICollectionViewCell .然后调用UIViewController上的礼物通过弱引用传递而不是在 UICollectionViewCell 的实例上调用它.就是这样:

cellForItem方法:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifier, for: indexPath) as! MessagesViewCell
cell.controller = self
//...
return cell
}

MessagesViewCell :
class MessagesViewCell: UICollectionViewCell, MFMailComposeViewControllerDelegate {
weak var controller: UIViewController?
func showMailComposer() {
guard MFMailComposeViewController.canSendMail() else {
return
}

let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["email"])
composer.setSubject("")
composer.setMessageBody("", isHTML: false)
composer.present(composer, animated: true)
controller?.present(composer, animated: true)
}

关于ios - 尝试从应用程序快速发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62293414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com