Learn Courses My Dashboard

Sending email from form

I found several pages and video’s on creating simple ‘contact us’ forms for swift. They all have the same steps and I followed them pretty carefully. But when I run the app (on my phone, not on simulator) when I fill out all the fields and click the ‘send’ button, the application closes and nothing gets sent. I DO have a warning on my “received actions” segues, “sendMessage: Send Message Touch Up Inside” has a warning on it that says ‘ssendMessage is not defined on ‘contactViewController’’, but it’s defined in an IBAction… so I’m clueless. Any suggestions?

import UIKit
import MessageUI

class ContactViewController: UIViewController, MFMailComposeViewControllerDelegate {

    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var phoneField: UITextField!
    @IBOutlet weak var messageField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func sendMessage(sender: AnyObject) {
        let toRecipients = ["me@email.com"]
        let mc: MFMailComposeViewController = MFMailComposeViewController()
        mc.mailComposeDelegate = self
        mc.setToRecipients(toRecipients)
        mc.setSubject("MESSAGE FROM KMA APP")
        mc.setMessageBody("Name: \(nameField.text!) \n\nEmail: \(emailField.text!) \n\nPhone: \(phoneField.text!) \n\nMessage: \(messageField.text!)", isHTML: false)
        self.present(mc, animated: true, completion: nil)
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }
    
    @IBAction func dismissKeyboard(_ sender: Any) {
    self.resignFirstResponder()
    }
}

When the app terminates I’m getting this in debug area:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[KBA.ContactViewController sendMessage:]: unrecognized selector sent to instance 0x104217e90'
terminating with uncaught exception of type NSException

That sounds like you haven’t connected the IBAction correctly

Is this IBAction for sendMessage connected to ContactViewController?? You can right click on the view controller in storyboard and you should see the connection