gpt4 book ai didi

ios - 如何验证 UIAlertController 中的 TextFields?

转载 作者:IT王子 更新时间:2023-10-29 05:13:41 26 4
gpt4 key购买 nike

谁能告诉我如何在 UIAlertController 中验证 UITextFields

我需要它来防止用户单击“保存”,除非输入两个字段。

到目前为止,这是我的代码:

@IBAction func btnStart(sender: AnyObject) {
var alert = UIAlertController(title: "New user",
message: "Add a new user",
preferredStyle: .Alert)

let saveAction = UIAlertAction(title: "Save",
style: .Default) { (action: UIAlertAction!) -> Void in

self.textFieldName = alert.textFields![0] as UITextField
self.textFieldEmail = alert.textFields![1] as UITextField
self.saveUser(self.textFieldName.text, email: self.textFieldEmail.text)
self.tableView.reloadData()
}

saveAction.enabled = false

let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction!) -> Void in
}

alert.addTextFieldWithConfigurationHandler {
(textFieldName: UITextField!) in
textFieldName.placeholder = "Enter full name"
}

alert.addTextFieldWithConfigurationHandler {
(textFieldEmail: UITextField!) in
textFieldEmail.placeholder = "Enter valid email adress"
textFieldEmail.keyboardType = .EmailAddress

}
alert.addAction(saveAction)
alert.addAction(cancelAction)

presentViewController(alert,
animated: true,
completion: nil)
}

这是我验证电子邮件字段的函数:

func isValidEmail(testStr:String) -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"

if let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) {
return emailTest.evaluateWithObject(testStr)
}
return false
}

最佳答案

这可以通过扩展 UIAlertViewController 来完成:

extension UIAlertController {

func isValidEmail(_ email: String) -> Bool {
return email.characters.count > 0 && NSPredicate(format: "self matches %@", "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,64}").evaluate(with: email)
}

func isValidPassword(_ password: String) -> Bool {
return password.characters.count > 4 && password.rangeOfCharacter(from: .whitespacesAndNewlines) == nil
}

func textDidChangeInLoginAlert() {
if let email = textFields?[0].text,
let password = textFields?[1].text,
let action = actions.last {
action.isEnabled = isValidEmail(email) && isValidPassword(password)
}
}
}

// ViewController
override func viewDidLoad() {
super.viewDidLoad()

let alert = UIAlertController(title: "Please Log In", message: nil, preferredStyle: .alert)

alert.addTextField {
$0.placeholder = "Email"
$0.addTarget(alert, action: #selector(alert.textDidChangeInLoginAlert), for: .editingChanged)
}

alert.addTextField {
$0.placeholder = "Password"
$0.isSecureTextEntry = true
$0.addTarget(alert, action: #selector(alert. textDidChangeInLoginAlert), for: .editingChanged)
}

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))

let loginAction = UIAlertAction(title: "Submit", style: .default) { [unowned self] _ in
guard let email = alert.textFields?[0].text,
let password = alert.textFields?[1].text
else { return } // Should never happen

// Perform login action
}

loginAction.isEnabled = false
alert.addAction(loginAction)
present(alert, animated: true)
}

enter image description here

关于ios - 如何验证 UIAlertController 中的 TextFields?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30596851/

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