[英]Swift - How to use integrate SwiftValidator into my project
I've got a login form which takes 2 alternate forms dependent on how a user taps a segmented control .... either as a register form with email, password, and repeat password; 我有一个登录表单,该表单有2种备用表单,具体取决于用户如何点击分段控件....作为带有电子邮件,密码和重复密码的注册表单; or as a login form with just email and password. 或作为仅包含电子邮件和密码的登录表单。
When a user fills out the register or login fields and taps the register/login button ... I log the user in using code along the lines of ... 当用户填写注册或登录字段并点击注册/登录按钮时...我使用代码沿...的行登录用户...
@IBAction func loginRegisterTapped(sender: AnyObject) {
//check whether the sender tag is registerTag (in which case a new user is registering or alternately returning user logging in
//do various checks and if all good submit the form
}
Ive recently copied over a validation library (SwiftValidator) to my project, and configured it to also validate fields when the login/register button is tapped ie i insert the validation code above my own code in the loginRegisterTapped ibaction method. Ive最近将验证库(SwiftValidator)复制到我的项目中,并将其配置为在点击登录/注册按钮时也验证字段,即在我自己的代码上方将验证代码插入loginRegisterTapped ibaction方法中。 My code after integrating SwiftValidator essentially becomes 集成SwiftValidator之后,我的代码本质上变成了
@IBAction func loginRegisterTapped(sender: AnyObject) {
//validation
self.clearErrors()
validator.validate(self)
//check whether the sender tag is registerTag (in which case a new user is registering or alternately returning user logging in
//do various checks and if all good submit the form
}
The SwiftValidator library ends us calling 2 delegate methods (in my LoginViewController) on validation success or failure as follows SwiftValidator库结束时,我们在验证成功或失败时调用了2个委托方法(在我的LoginViewController中),如下所示
// SwiftValidator library Delegate methods
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
}
}
Given validation is successful, how can i get code to return to my @IBAction func loginRegisterTapped(sender: AnyObject) method and continue with my own form submission code (... I need a reference to the sender to check it's tag to determine whether the button was tapped as a "Register" button or a "Login" button) rather than conducting the form submission as suggested by the SwiftValidator library within it's validationSuccessful delegate method where i dont have access to the sender var. 给定验证成功,我如何获取代码以返回到我的@IBAction函数func loginRegisterTapped(sender:AnyObject)方法,并继续执行自己的表单提交代码(...我需要引用发送方以检查其标签以确定是否该按钮被点击为“注册”按钮或“登录”按钮),而不是如SwiftValidator库所建议的那样在其ValidationSuccessful委托方法中进行表单提交,而我无法访问发送方var。 ie what options do i have here or what would be considered best practice? 即,我在这里有什么选择或被认为是最佳实践? ... I thought of customizing the validationSuccessful method to return perhaps a bool to the calling function but doubt if that's the best way? ...我想自定义validationSuccessful方法以将布尔值返回给调用函数,但怀疑这是否是最佳方法?
IMHO, I'd do the successful form submission in the validationSuccessful()
method, not in your own action method. 恕我直言,我会使用validationSuccessful()
方法而不是您自己的操作方法来成功提交表单。 I'd avoid using tags to differentiate the login and register buttons, and instead have each one call a separate action method. 我会避免使用标签来区分登录按钮和注册按钮,而是让每个调用单独的操作方法。 You're not saving any time, code, or logical clarity by handling them both with the same handler. 通过使用相同的处理程序来处理它们,不会节省任何时间,代码或逻辑清晰度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.