Peter Boesen Peter Boesen - 6 months ago 17
iOS Question

Clearing UITextfields with UIButton

i have a page with 5 UITextfields and 1 UItextview. These are a email form, and i want it so when you press the button that sends the email, it also clears the textfields and textview. It is very important that it only does this after the data has been collected for the email, so the email won't be empty.

Plz answer precisely, and with clear instructions, since i am very new to the whole xcode/swift thing.Thank you :)

code:

@IBOutlet var text1: UITextField!
@IBOutlet var text2: UITextField!
@IBOutlet var text3: UITextField!
@IBOutlet var text4: UITextField!
@IBOutlet var text5: UITextField!
@IBOutlet var text6: UITextView!

override func viewDidLoad() {
super.viewDidLoad()

self.text1.delegate = self
self.text2.delegate = self
self.text3.delegate = self
self.text4.delegate = self
self.text5.delegate = self

// Do any additional setup after loading the view.

}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func sendEmail(sender: AnyObject) { let mailComposeViewController = configuredMailComposeViewController()



if MFMailComposeViewController.canSendMail() {
self.presentViewController(mailComposeViewController, animated: true, completion: nil)
} else {
self.showSendMailErrorAlert()
}
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self // Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property
let messageBody = text1.text!+"\n"+text2.text!+"\n"+text3.text!+"\n"+text4.text!+"\n"+text5.text!+"\n"+text6.text!
mailComposerVC.setToRecipients(["wastepin@hotmail.com"])
mailComposerVC.setSubject("New Dumpster")
mailComposerVC.setMessageBody(messageBody, isHTML: false)
return mailComposerVC


Picture of page:
Mail form page

Answer

Try this

for textField in [text1, text2, text3,text4, text5] {
    textField.text = ""
}

Because text6 is a UITextView, put it separately.

text6.text = ""
Comments