我的电子邮件工作正常。我唯一需要做的就是在我的邮件正文中添加一个额外的字符串。例如,我想将Name:和textfield添加到我的邮件正文中。像这样的东西。
姓名:John Smith
电话:566-654-6577
电子邮件:Smith@smith.com
现在,邮件正文仅显示以下内容。
John Smith
566-654-6577
Smith@smith.com
import UIKit
import MessageUI
class EmailTableViewController: UITableViewController, MFMailComposeViewControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var name: UITextField!
@IBOutlet weak var phone: UITextField!
@IBOutlet weak var email: UITextField!
func appendTextFromTextField(string: String, textField: UITextField) -> String {
return string + textField.text + "\n \n"
}
@IBAction func SendEmailButton(sender: AnyObject) {
var fields: [UITextField] = [name, phone, email]
var messageBody = ""
for f in fields {
messageBody = appendTextFromTextField(messageBody, textField: f)
}
var emailTitle = "Interface Information"
var toRecipents = [""]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}
func mailComposeController(controller:MFMailComposeViewController, didFinishWithResult result:MFMailComposeResult, error:NSError) {
switch result.value {
case MFMailComposeResultCancelled.value:
println("Mail cancelled")
case MFMailComposeResultSaved.value:
println("Mail saved")
case MFMailComposeResultSent.value:
println("Mail sent")
case MFMailComposeResultFailed.value:
println("Mail sent failure: %@", [error.localizedDescription])
default:
break
}
self.dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
答案 0 :(得分:1)
删除for
循环并尝试用以下方法替换您的方法:
@IBAction func sendEmailButton(sender: UIButton) {
var messageBody = "Name:\(name.text)\nPhone:\(phone.text)\nEmail:\(email.text) "
var emailTitle = "Interface Information"
var toRecipents = [""]
var mc: MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(emailTitle)
mc.setMessageBody(messageBody, isHTML: false)
mc.setToRecipients(toRecipents)
self.presentViewController(mc, animated: true, completion: nil)
}
答案 1 :(得分:0)
您可以使用reduce
和zip2
函数执行此类操作。
let fields: [UITextField] = [name , phone, email]
let fieldNames : [String] = ["Name", "Phone" ,"Email"]
let messageBody = reduce(Zip2(fields,fieldNames),"") { body,zipped in
let (field,fieldName) = zipped
return body + fieldName + " : " + field.text + " \n\n "
}
答案 2 :(得分:0)
替换 var字段:[UITextField] = [姓名,电话,电子邮件] var messageBody =“”
for f in fields {
messageBody = appendTextFromTextField(messageBody, textField: f)
}
使用: var messageBody =“” let addOn:String =“name:(name.text)\ nphone:(phone.text)\ nemail:(email.text)\ n” messageBody = messageBody + addOn
另外,我不确定为什么你有一个tableViewController。如果您没有在故事板中放置表格,则会出现运行时错误