无法从文本字段获取字符串变量以传递到邮件按钮功能 [英] Cannot get string variables from textfield to pass into mail button function

查看:40
本文介绍了无法从文本字段获取字符串变量以传递到邮件按钮功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,允许用户输入他们的联系信息,并将其包含在文本字段中的信息发送到我的电子邮件地址.一切似乎都正常,我可以使用所需的文本发送邮件,但是似乎无法从文本字段中输入信息.请帮忙!我对此有点陌生:)

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! \nMy name or business is: \(name) \nMy contact information is: \(contact) \nMy 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):

另一个朋友!

我的名字或公司是:无

我的联系信息是:无

我的其他信息包括:无"

My additional information includes: nothing"

已将我的 sendMail 函数更改为仅包含出口名称:

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! \nMy name or business is: \(nameField.text) \nMy contact information is: \(contactField.text) \nMy additional information includes: \(otherField.text)", isHTML: false)

    return mailComposerVC
}

现在我的输出中包含文本字段信息:

And now my output has textfield info:

另一个朋友!

我的名字或公司是:Optional(无论我在文本字段中输入什么")

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")"

但是如何摆脱字符串周围的 Optional(")?

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

推荐答案

学习如何处理可选变量是学习Swift的重要组成部分.有几种方法可以执行您要尝试执行的操作.如果可以解决某些字段,如果可以的话,可以使用nil合并运算符设置默认值.例如:

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.

这篇关于无法从文本字段获取字符串变量以传递到邮件按钮功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆