You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击Contact按钮触发黑屏,伴KVC报错,邮件编辑器无法显示

Fixing Your Email Composer Issues: Black Screen & KVC Error

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 keyPath as 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 MFMailComposeViewController to UIViewController
  • Remove the unused contact() call in your Send_Tapped method (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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:22:57