点击Contact按钮触发黑屏,伴KVC报错,邮件编辑器无法显示
Let's tackle both of your problems one by one, like we're debugging together:
1. Resolve the keyPath KVC Error
The error message this class is not key value coding-compliant for the key keyPath is a classic clue: somewhere in your Storyboard/XIB, your Contact button has a User Defined Runtime Attribute set for a key named keyPath, but your code doesn't define this property.
Here's how to fix it:
- Open your Storyboard/XIB file
- Select the Contact button that triggers the email action
- Switch to the Identity Inspector (third tab on the right sidebar)
- Scroll down to User Defined Runtime Attributes
- Look for any entry with
keyPathas the Key — delete that entry completely
This will stop the error from popping up when you use the SWReveal slide menu.
2. Fix the Black Screen When Tapping "Contact"
The root cause of the black screen is a wrong inheritance choice: your SendEmailVC is subclassing MFMailComposeViewController, but you shouldn't inherit from this controller. Instead, you should create an instance of it and present it normally.
Here's the fix:
- Change your class inheritance from
MFMailComposeViewControllertoUIViewController - Remove the unused
contact()call in yourSend_Tappedmethod (it's not defined anywhere, so it's just dead code)
Modified Working Code
import Foundation import UIKit import MessageUI class SendEmailVC: UIViewController, MFMailComposeViewControllerDelegate { @IBAction func Send_Tapped(_ sender: Any) { if MFMailComposeViewController.canSendMail() { let mailComposeViewController = configureMailController() self.present(mailComposeViewController, animated: true, completion: nil) } else { showMailError() } } func configureMailController() -> MFMailComposeViewController { let mailComposerVC = MFMailComposeViewController() mailComposerVC.mailComposeDelegate = self mailComposerVC.setToRecipients(["testing@gmail.com"]) mailComposerVC.setSubject("Hello") mailComposerVC.setMessageBody("How are you doing?", isHTML: false) return mailComposerVC } /* * DON'T EDIT THE CODE BELOW. */ func showMailError() { let sendMailErrorAlert = UIAlertController(title: "Email failed to send", message: "Your device fail to send the email", preferredStyle: .alert) let dismiss = UIAlertAction(title: "Dale", style: .default, handler: nil) sendMailErrorAlert.addAction(dismiss) self.present(sendMailErrorAlert, animated: true, completion: nil) } func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { controller.dismiss(animated: true, completion: nil) } }
Quick Extra Check
Double-check that your SendEmailVC is properly linked in the Storyboard: make sure the view controller's class is set correctly in the Identity Inspector, and the Send_Tapped action is connected to your Contact button.
内容的提问来源于stack exchange,提问作者Palermox

