我正在使用这个简单的表单来处理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
}}
发布于 2015-04-25 10:12:38
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
https://stackoverflow.com/questions/29340708
复制相似问题