Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

ios - Cannot get string variables from textfield to pass into mail button function

I have an app that allows the user to input their contact information and it sends the mail to my email address with the information they put into the textfields. Everything seems to work and I can send the mail with the desired text, however I can't seem to input the information from the textfield. Please help! Im kind of new to this :)

var name: String?
@IBOutlet weak var nameField: UITextField!

var contact: String?
@IBOutlet weak var contactField: UITextField!

var other: String = "nothing"
@IBOutlet weak var otherField: UITextField!


@IBAction func sendEmail(_ sender: Any) {

    let name = nameField.text

    let contact = contactField.text

    if other == nil {

    }else{
        let other = otherField.text!
    }

When I click the button, it pulls up the mail app with information in the body. Here is the code:

func configureMailController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients(["email@gmail.com"])
    mailComposerVC.setSubject("Contact information")
    mailComposerVC.setMessageBody("Another Friend! 
My name or business is: (name) 
My contact information is: (contact) 
My additional information includes: (other)", isHTML: false)

    return mailComposerVC
}

Here is the output in the body of the mail app (even when I write things in the textfield):

"Another Friend!

My name or business is: nil

My contact information is: nil

My additional information includes: nothing"

EDIT:

Ive change my sendMail function to simply contain the outlet names:

func configureMailController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients(["danielgannage@gmail.com"])
    mailComposerVC.setSubject("Staples Day")
    mailComposerVC.setMessageBody("Another Friend! 
My name or business is: (nameField.text) 
My contact information is: (contactField.text) 
My additional information includes: (otherField.text)", isHTML: false)

    return mailComposerVC
}

And now my output has textfield info:

"Another Friend!

My name or business is: Optional("whatever I put in the textfield")

My contact information is: Optional("whatever I put in the textfield")

My additional information includes: Optional("whatever I put in the textfield")"

But how do I get rid of the: Optional("") surrounding my string?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Learning how to handle optional variables is an essential part of learning Swift. There are several methods to do what you are trying to do. If you are okay if some of these fields being unanswered, you could use a nil coalescing operator to set a default value. For example:

let name = nameField.text ?? "unknown"

Alternately, if it is no good without an answer, you could guard against that case:

guard let contact = contactField.text else { 
    // display missing info error 
    return
}

This would end the function, and not call the email client.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...