在 swift 4 中正确显示所有文本字段是否完整?
Show message if all textfields are complete, correctly in swift 4?
我正在创建一个电子邮件输入和一个姓名输入,我需要在所有字段都完成后显示一条消息。但出于某种原因,当存在空白字段并且用户单击提交按钮时,将显示成功消息。有没有办法在所有字段完成后放入一个子句或其他东西来触发它?谢谢您的帮助。
这是我的代码:
import UIKit
import MessageUI
class RequestFilterVC: UIViewController {
@IBOutlet weak var emailTxtField: UITextField!
@IBOutlet weak var filterTxtField: UITextField!
@IBOutlet weak var requestBtn: UIButton!
@IBOutlet weak var validatorMessage: UILabel!
@IBOutlet weak var requestedMessage: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// hide validator message
validatorMessage.isHidden = true
requestedMessage.isHidden = true
}
@IBAction func requestBtnWasTapped(_ sender: Any) {
let providedEmailAddress = emailTxtField.text
let isEmailAddressValid = isValidEmailAddress(emailAddressString: providedEmailAddress!)
if isEmailAddressValid
{
print("Email address is valid")
} else {
print("Email address is not valid")
displayAlertMessage(messageToDisplay: "Email address is not valid")
}
// If All are completed then send the email .
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self as? MFMailComposeViewControllerDelegate
// Configure the fields of the interface.
composeVC.setToRecipients(["myemail@awesomeemail.com])
composeVC.setSubject("Form Submit)
composeVC.setMessageBody("\(emailTxtField, filterTxtField)", isHTML: false)
// Present the view controller modally
// self.present(composeVC, animated: true, completion: nil)
requestedMessage.isHidden = false
requestedMessage.text = "Submitted form . thank you"
}
func isValidEmailAddress(emailAddressString: String) -> Bool {
var returnValue = true
let emailRegEx = "[A-Z0-9a-z.-_]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,3}"
do {
let regex = try NSRegularExpression(pattern: emailRegEx)
let nsString = emailAddressString as NSString
let results = regex.matches(in: emailAddressString, range: NSRange(location: 0, length: nsString.length))
if results.count == 0
{
returnValue = false
}
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
returnValue = false
}
return returnValue
}
func displayAlertMessage(messageToDisplay: String)
{
let alertController = UIAlertController(title: "Error", message: messageToDisplay, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
print("Ok button tapped");
}
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion:nil)
}
}
使用正则表达式创建委托听起来怎么样?每次你按下一个键调用委托,如果它与正则表达式匹配,你可以取消隐藏 UILabel ......这样的事情可以工作。
改变
displayAlertMessage(messageToDisplay: "Email address is not valid")
至
displayAlertMessage(messageToDisplay: "Email address is not valid")
return
如果第二个文本字段为空或不在任何地方,您就不会检查ing。 (我不知道为什么你将一个应该存储名称的文本字段命名为 filterTextField
)
if isEmailAddressValid && !filterTextField.text?.isEmpty {
print("Email address is valid")
} else {
print("Email address is not valid")
displayAlertMessage(messageToDisplay: "Email address is not valid")
return
}
对于您的问题,以下更改将处理错误
验证电子邮件后,您不会返回或中断流程,它将执行您函数中剩下的剩余代码。所以你需要停止执行。
if isEmailAddressValid
{
print("Email address is valid")
} else {
print("Email address is not valid")
displayAlertMessage(messageToDisplay: "Email address is not valid")
}
How ever for better approach, I would suggest to disable the submit
button until the text field has value.
func textFieldDidBeginEditing(textField: UITextField!) { //delegate method
//check for the required text field
if (emailTxtField.text!.isEmpty){
//disable submit button
}
else{
// enable the submit button
}
}
func textFieldShouldEndEditing(textField: UITextField!) -> Bool { //delegate method
if (emailTxtField.text!.isEmpty){
//disable submit button
}
else{
// enable the submit button
}
return true
}
我正在创建一个电子邮件输入和一个姓名输入,我需要在所有字段都完成后显示一条消息。但出于某种原因,当存在空白字段并且用户单击提交按钮时,将显示成功消息。有没有办法在所有字段完成后放入一个子句或其他东西来触发它?谢谢您的帮助。
这是我的代码:
import UIKit
import MessageUI
class RequestFilterVC: UIViewController {
@IBOutlet weak var emailTxtField: UITextField!
@IBOutlet weak var filterTxtField: UITextField!
@IBOutlet weak var requestBtn: UIButton!
@IBOutlet weak var validatorMessage: UILabel!
@IBOutlet weak var requestedMessage: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// hide validator message
validatorMessage.isHidden = true
requestedMessage.isHidden = true
}
@IBAction func requestBtnWasTapped(_ sender: Any) {
let providedEmailAddress = emailTxtField.text
let isEmailAddressValid = isValidEmailAddress(emailAddressString: providedEmailAddress!)
if isEmailAddressValid
{
print("Email address is valid")
} else {
print("Email address is not valid")
displayAlertMessage(messageToDisplay: "Email address is not valid")
}
// If All are completed then send the email .
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self as? MFMailComposeViewControllerDelegate
// Configure the fields of the interface.
composeVC.setToRecipients(["myemail@awesomeemail.com])
composeVC.setSubject("Form Submit)
composeVC.setMessageBody("\(emailTxtField, filterTxtField)", isHTML: false)
// Present the view controller modally
// self.present(composeVC, animated: true, completion: nil)
requestedMessage.isHidden = false
requestedMessage.text = "Submitted form . thank you"
}
func isValidEmailAddress(emailAddressString: String) -> Bool {
var returnValue = true
let emailRegEx = "[A-Z0-9a-z.-_]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,3}"
do {
let regex = try NSRegularExpression(pattern: emailRegEx)
let nsString = emailAddressString as NSString
let results = regex.matches(in: emailAddressString, range: NSRange(location: 0, length: nsString.length))
if results.count == 0
{
returnValue = false
}
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
returnValue = false
}
return returnValue
}
func displayAlertMessage(messageToDisplay: String)
{
let alertController = UIAlertController(title: "Error", message: messageToDisplay, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
print("Ok button tapped");
}
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion:nil)
}
}
使用正则表达式创建委托听起来怎么样?每次你按下一个键调用委托,如果它与正则表达式匹配,你可以取消隐藏 UILabel ......这样的事情可以工作。
改变
displayAlertMessage(messageToDisplay: "Email address is not valid")
至
displayAlertMessage(messageToDisplay: "Email address is not valid")
return
如果第二个文本字段为空或不在任何地方,您就不会检查ing。 (我不知道为什么你将一个应该存储名称的文本字段命名为 filterTextField
)
if isEmailAddressValid && !filterTextField.text?.isEmpty {
print("Email address is valid")
} else {
print("Email address is not valid")
displayAlertMessage(messageToDisplay: "Email address is not valid")
return
}
对于您的问题,以下更改将处理错误 验证电子邮件后,您不会返回或中断流程,它将执行您函数中剩下的剩余代码。所以你需要停止执行。
if isEmailAddressValid
{
print("Email address is valid")
} else {
print("Email address is not valid")
displayAlertMessage(messageToDisplay: "Email address is not valid")
}
How ever for better approach, I would suggest to disable the submit button until the text field has value.
func textFieldDidBeginEditing(textField: UITextField!) { //delegate method
//check for the required text field
if (emailTxtField.text!.isEmpty){
//disable submit button
}
else{
// enable the submit button
}
}
func textFieldShouldEndEditing(textField: UITextField!) -> Bool { //delegate method
if (emailTxtField.text!.isEmpty){
//disable submit button
}
else{
// enable the submit button
}
return true
}