gpt4 book ai didi

ios - 如果所有文本字段都完整,则在 swift 4 中正确显示消息?

转载 作者:行者123 更新时间:2023-11-30 11:13:31 25 4
gpt4 key购买 nike

我正在创建电子邮件输入和姓名输入,我需要在所有字段填写完毕后显示一条消息。但由于某种原因,当存在空白字段并且用户单击提交按钮时,会显示成功消息。有没有办法在所有字段完成后放入一个子句或其他东西来触发它?谢谢您的帮助。

这是我的代码:

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)
}
}

最佳答案

您没有检查第二个文本字段是否为空或不在任何地方。 (我不知道为什么您将一个应该存储名称的文本字段命名为 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
}

关于ios - 如果所有文本字段都完整,则在 swift 4 中正确显示消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51954309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com