如何通过Swift与多个UITextField
撰写电子邮件?我似乎只能在UITextField
下输入一个数据(名为messageBody
)。如何向UITextField
添加多个messageBody
?
import UIKit
import CoreData
import MessageUI
class EmailTableViewController: UITableViewController, MFMailComposeViewControllerDelegate, UITextFieldDelegate {
@IBOutlet weak var name: UITextField!
@IBOutlet weak var phone: UITextField
@IBOutlet weak var email: UITextField!
@IBOutlet weak var base: UITextField!
@IBOutlet weak var rig: UITextField!
@IBOutlet weak var wellhead: UITextField!
@IBOutlet weak var connector: UITextField!
@IBOutlet weak var size: UITextField!
@IBOutlet weak var depth: UITextField!
@IBOutlet weak var pressure: UITextField!
@IBOutlet weak var temp: UITextField!
@IBAction func SendEmailButton(sender: AnyObject) {
var emailTitle = "Interface Information"
var messageBody = name.text
var toRecipents = ["test.com"]
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)
}
答案 0 :(得分:3)
您可以创建一个小辅助函数并将字段放在数组中,如下所示:
func appendTextFromTextField(string: String, textField: UITextField) -> String {
return string + textField.text + "\n"
}
@IBAction func SendEmailButton(sender: AnyObject) {
var fields: [UITextField] = [name, phone, email, base, rig, wellhead, connector,
size, depth, pressure, temp]
var messageBody = ""
for f in fields {
messageBody = appendTextFromTextField(messageBody, textField: f)
}
// etc.
}