gpt4 book ai didi

ios - Swift 中的 XLForm 验证

转载 作者:行者123 更新时间:2023-11-28 07:08:50 28 4
gpt4 key购买 nike

我正在使用 XLForm 处理这个简单的表单。代码是用 Swift 编写的。我在验证方面遇到问题 - 我想对电子邮件和其他字段使用 XLForm 的内部验证器,但我不知道如何操作。我只需要检查其他字段是否填充了数据。手册是用 Obj-C 编写的,我在 Swift 中找不到任何示例。谁能给我一些如何实现的提示?我正在尝试使用 userEmail.required = true 但它不起作用。我一直在寻找一些在 saveTapped 方法中实现的方法,以便在我发送表单之前验证字段,但我找不到任何解决方案。

class FormViewController: XLFormViewController {


required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder);
self.setupForm()
}

override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func saveTapped(sender: AnyObject) {


println(form.formRowWithTag("userEmail").value as? String)
println(form.formRowWithTag("userPassword").value as? String)
println(form.formRowWithTag("userName").value as? String)

}

private func setupForm() {

let form = XLFormDescriptor(title: "Registration")

// Section 1
let section1 = XLFormSectionDescriptor.formSection() as XLFormSectionDescriptor
form.addFormSection(section1)

let userEmail = XLFormRowDescriptor(tag: "userEmail", rowType: XLFormRowDescriptorTypeText, title: "Email")
userEmail.required = true
section1.addFormRow(userEmail)

let userPassword = XLFormRowDescriptor(tag: "userPassword", rowType: XLFormRowDescriptorTypePassword, title: "Password")
userPassword.required = true
section1.addFormRow(userPassword)


let userName = XLFormRowDescriptor(tag: "userName", rowType: XLFormRowDescriptorTypePassword, title: "First name")
userName.required = true
section1.addFormRow(userName)


self.form = form
}

最佳答案

    let validationErrors:NSArray = self.formValidationErrors()
if (validationErrors.count > 0) {
var errorString = ""
for error in validationErrors {
errorString += error.localizedDescription + "\n"
}
UIAlertView(title: "Error! Please check again.", message: errorString, delegate: nil, cancelButtonTitle: "OK").show()
return false
}

将此代码放入您的 saveTapped

关于ios - Swift 中的 XLForm 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29340708/

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