【发布时间】:2018-08-16 16:25:52
【问题描述】:
我有这个代码,它关于 didFinishWith 结果的问题没有被调用并且应用程序崩溃。这是我的代码
注意:在 Objective C 中,这段代码可以正常工作,因为我在我的类中创建了一个强引用,但我在 Swift 中遇到了问题,我不知道如何解决这个问题
import Foundation
import MessageUI
class EmailCreator: NSObject, MFMailComposeViewControllerDelegate {
// in other class I send this var to show email
var viewToShow = UIViewController ()
func sendEmail() {
let mailComposeViewController = createMailComposeViewController()
if MFMailComposeViewController.canSendMail(){
viewToShow.present(mailComposeViewController, animated: true, completion: nil)
}else{
print("Can't send email")
}
}
func createMailComposeViewController() -> MFMailComposeViewController {
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.mailComposeDelegate = self
mailComposeViewController.setToRecipients(["example@test.test"])
mailComposeViewController.setSubject("subject")
mailComposeViewController.setMessageBody("test body", isHTML: false)
return mailComposeViewController
}
//MARK: - MFMail compose method
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
}
在其他班级我有这个代码来显示电子邮件:
@IBAction func sendEmail(_ sender: UIButton) {
let email = EmailCreator()
email.viewToShow = self
email.sendEmail()
}
【问题讨论】:
-
什么是崩溃和堆栈跟踪?
-
线程 1:EXC_BAD_ACCESS(代码=1,地址=0xb6f4ebeb8)
-
编辑您的问题并在那里添加整个内容。我不能仅仅用它来帮助你。
-
控制台崩溃时是否有日志?另外,导致崩溃的线路是什么?此外,您正在创建一个
UIViewController只是为了替换它,为什么不这样做var viewToShow: UIViewController??然后viewToShow?.present(mailComposeViewController, animated: true, completion: nil) -
我在 cosole 中没有日志。应用程序显示此错误:线程 1:EXC_BAD_ACCESS (code=1, address=0xb6f4ebeb8)