【问题标题】:Cannot get string variables from textfield to pass into mail button function无法从文本字段中获取字符串变量以传递给邮件按钮功能
【发布时间】:2018-12-05 16:27:16
【问题描述】:

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

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!
    }

当我单击按钮时,它会拉出邮件应用程序,其中包含正文中的信息。代码如下:

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
}

这是邮件应用程序正文中的输出(即使我在文本字段中写东西):

“另一个朋友!

我的名字或公司是:无

我的联系方式是:无

我的附加信息包括:没有”

编辑:

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

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
}

现在我的输出有文本字段信息:

“另一个朋友!

我的名字或公司是:可选的(“我在文本字段中输入的任何内容”)

我的联系信息是:可选的(“我在文本字段中输入的任何内容”)

我的附加信息包括:可选(“我在文本字段中输入的任何内容”)”

但是我如何摆脱围绕我的字符串的:Optional("")

【问题讨论】:

  • 您永远不会设置 namecontactother 属性。这与your earlier question中的代码中的问题相同。
  • 我不确定你的意思。那些变量的属性不是在sendEmail func 中设置的吗?
  • 不,您设置的局部变量恰好与您的属性同名。
  • 所以我的属性是文本字段的出口?我将如何设置属性?
  • let name = nameField.text 更改为name = nameField.text

标签: ios swift string button textfield


【解决方案1】:

学习如何处理可选变量是学习 Swift 的重要组成部分。有几种方法可以做你想做的事情。如果这些字段中的某些字段没有得到答复,您可以使用 nil 合并运算符来设置默认值。例如:

let name = nameField.text ?? "unknown"

或者,如果没有答案就不行,你可以提防这种情况:

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

这将结束函数,而不是调用电子邮件客户端。

【讨论】:

  • 谢谢! “未知”必须是未知的还是可以是空白:“”或引号中的任何其他单词。想知道引号是什么意思?这是否意味着如果文本字段中有 nil 则使用此字符串代替?
  • 它可以是您选择的任何字符串。如果 .text 值为 nil,则它只是一个默认值。 nil 合并运算符(??)本质上是对之前的表达式求值,如果求值为 nil,则使用第二个值。引号的唯一原因是这是一个字符串值。如果您有一个名为 someInt 且类型为 Int 的变量? (可选整数),你可以有value = someInt ?? 0,默认为零。
【解决方案2】:

只需添加!最后像 让contact = contactField.text!

【讨论】:

  • 我们为什么需要!到最后?
  • 因为它是可选的。你真的应该接受这个答案。即使这可行,但如果该值为 nil,它也很容易崩溃,因为您正在强制解包一个可选项。相反,请参阅 GntlmnBndt 的答案。
  • –1 因为您建议的做法非常糟糕。您应该了解有关选项的更多信息并使用 GntlmnBndt 的答案中建议的方法。 @AlibEzbar
  • let value = edTest.text // 为空总是返回 "" edTest.text = nil // 好的,让我们试试 nil print("value: +(value!)+") // 魔法。没有崩溃。只是 ++ 结果
猜你喜欢
  • 2018-07-19
  • 1970-01-01
  • 2011-08-01
  • 2016-12-03
  • 2021-11-23
  • 2020-10-27
  • 1970-01-01
  • 2021-05-15
  • 2018-09-05
相关资源
最近更新 更多