我正在尝试使用此库SwiftValidator
(https://github.com/jpotts18/SwiftValidator)在swift中验证密码,但我被抛出此错误:
参数标签'(委托:)'与任何可用的重载都不匹配
我该如何解决?另外,我可以使用AlertView来显示我的消息而不是标签吗? (在errorLabel: errorMsg
)
这是我的代码:
import Foundation
import UIKit
import SwiftValidator
import Alamofire
class ResetPasswordViewController: UIViewController, UITextFieldDelegate {
let validator = Validator()
@IBOutlet weak var errorMsg: UILabel!
@IBOutlet weak var usernameRP: UITextField!
@IBOutlet weak var passwordRP: UITextField!
@IBOutlet weak var confirmPasswordRP: UITextField!
@IBAction func submitBtnRP(sender: AnyObject) {
validator.validate(delegate:self)
}
override func viewDidLoad() {
super.viewDidLoad()
usernameRP.delegate = self
passwordRP.delegate = self
validator.registerField(usernameRP, rules: [RequiredRule(), MinLengthRule(length: 4)])
validator.registerField(passwordRP, rules: [RequiredRule(), MinLengthRule(length: 6)])
validator.registerField(confirmPasswordRP, errorLabel: errorMsg, rules: [ConfirmationRule(confirmField: passwordRP)])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func validationSuccessful() {
// submit the form
}
func validationFailed(errors:[UITextField:ValidationError]) {
// turn the fields to red
for (field, error) in validator.errors {
field.layer.borderColor = UIColor.redColor().CGColor
field.layer.borderWidth = 1.0
error.errorLabel?.text = error.errorMessage // works if you added labels
error.errorLabel?.hidden = false
}
}
}
答案 0 :(得分:1)
只需点击按钮点击下面的行:
validator.validate(self)
并像这样绑定其验证委托
class testViewController: UIViewController, ValidationDelegate
{
}
并且是的,您也可以向用户发出警报,但如果条件未符合条件,则可以逐一显示每个警报。