swift - IBOutlet UIButton SIGABRT

标签 swift xcode

我创建了一个 IOS 应用程序来发送电子邮件。我发现的问题是在 IOS 模拟器上测试我的应用程序。当我点击我的 UIButton (IBOulet UIButton) 时,我在 AppDelegate.swift 文件中收到 SIGABRT Error 高亮类。下面没有显示错误代码。我还检查了 View Controller 上的输出连接,没有感叹号(“!”)或现有的重复项。

@IBOutlet var Subject: UITextField!

@IBOutlet var Body: UITextView!

override func viewDidLoad(){
super.viewDidLoad()

    Subject.delegate = self
    Body.delegate = self
}


@IBAction func SendEmail (_sender: Any)
{
    let picker = MFMailComposeViewController()
    picker.mailComposeDelegate = self

    if let subjectText = Subject.text {
        picker.setSubject(subjectText)
    }
    picker.setMessageBody(Body.text, isHTML: true)

    present(picker, animated: true, completion: nil)
}

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    dismiss(animated: true, completion: nil)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()

    return true
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    Body.text = textView.text
    if text == "\n" {
        textView.resignFirstResponder()

        return false
    }
    return true
}



}

Outlet Connections

SIGABRT Error

最佳答案

在显示您的 MFMailComposeViewController 之前,您没有调用 MFMailComposeViewController.canSendMail()。来自 canSendMail()文档:

You should call this method before attempting to display the mail composition interface. If it returns false, you must not display the mail composition interface.

canSendMail() 如果您没有在您的设备上设置用于发送邮件的电子邮件地址,则将返回 false

尝试:

@IBAction func SendEmail (_sender: Any)
{
    if(!MFMailComposeViewController.canSendMail()) {
        if let url = URL(string: "mailto:" + yourEmailAddress) {
            UIApplication.shared.openURL(url)
        }
    }

    let picker = MFMailComposeViewController()
    picker.mailComposeDelegate = self

    if let subjectText = Subject.text {
        picker.setSubject(subjectText)
    }
    picker.setMessageBody(Body.text, isHTML: true)
}

您检查是否可以显示 MFMailComposerViewController,如果不能,您将打开一个 Mail 应用

关于swift - IBOutlet UIButton SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54458532/

相关文章:

ios - 将一个变量从一个 View Controller 传递到另一个 View Controller

ios - swift ,iOS 8+ 框架

ios - ios(Swift)中的选项控件

ios - UICollectionView 多个单元格选择 Swift iOS

ios - xcode: 错误 ITMS-90022: "Missing required icon file.."

ios - 如何让密码自动填充与 ASWebAuthenticationSession 一起使用?

ios - 使用 AVFoundation 播放 wav 声音文件

ios - 检查设备是否运行 iOS 5 或更高版本

Swift:无法将类型 'Double' 的值转换为预期的参数类型 'CGFloat' 但它可以吗?

xcode - swift : The smallest excutable for-loop