使用UIButton

时间:2016-05-29 17:58:37

标签: ios xcode swift uibutton uitextfield

我有一个包含5个UITextfields和1个UItextview的页面。这是一个电子邮件表单,我想要它,所以当你按下发送电子邮件的按钮时,它也会清除文本字段和textview。非常重要的是,它只在为电子邮件收集数据后执行此操作,因此电子邮件不会为空。

代码:

@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

页面图片: Mail form page

2 个答案:

答案 0 :(得分:0)

在提交时调用的函数中,首先将字段文本的值存储到变量中,然后将所有字段的文本设置为nil。此时您将获得信息,字段将为空。

答案 1 :(得分:0)

试试这个

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

由于text6UITextView,请将其单独放置。

text6.text = ""