【发布时间】: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("")?
【问题讨论】:
-
您永远不会设置
name、contact和other属性。这与your earlier question中的代码中的问题相同。 -
我不确定你的意思。那些变量的属性不是在
sendEmailfunc 中设置的吗? -
不,您设置的局部变量恰好与您的属性同名。
-
所以我的属性是文本字段的出口?我将如何设置属性?
-
将
let name = nameField.text更改为name = nameField.text。
标签: ios swift string button textfield