我想在我的视图中实现验证,当用户在UITextField
中输入无效文本时,所有其他UITextField
都被禁用,光标仍保留在当前位置。我使用的是SwiftValidator
库(https://github.com/jpotts18/SwiftValidator)。
文本字段的验证由UIControlEvent.EditingDidEnd
触发,通常在用户移动到下一个字段时发生。我无法阻止光标移动到另一个UITextField
,也无法将其恢复到验证失败的视图。什么是正确的方法?
以下是我的代码的相关部分,其中validateSingleValue()
是我正在尝试为此任务实现逻辑的地方。
我的ViewController
充当我的自定义控件组的代理,处理EditingDidEnd
个事件。自定义控件通常是UILabel
中嵌入的UITextField
和UIView
。
func textViewEditingDidEnd(editTextPropertyGroup: EditTextPropertyGroup) {
validateField(editTextPropertyGroup.propertyValue)
}
使用
验证字段func validateField(sender: AnyObject) {
switch (sender as! UITextField) {
case firstNameField:
validateSingleValue(textfield: firstNameField, validationrule: firstNameRule(), errorlabel: firstNameErrorLabel, container: firstNameField.superview)
case lastNameField:
validateSingleValue(textfield: lastNameField, validationrule: lastNameRule(), errorlabel: lastNameErrorLabel, container: lastNameField.superview)
case adressField:
validateSingleValue(textfield: addressField, validationrule: addressRule(), errorlabel: addressErrorLabel, container: addressField.superview)
case emailField:
validateSingleValue(textfield: emailField, validationrule: emailRule(), errorlabel: emailErrorLabel, container: emailField.superview)
default:
print("No fields to validate.")
}
}
func validateSingleValue(textfield textField:UITextField, validationrule validationRule: Rule, errorlabel errorLabel: UILabel, container: UIView?) -> Bool {
if validationRule.validate(textField.text!) {
errorLabel.hidden = true
textField.layer.borderColor = UIColor.grayColor().CGColor
textField.layer.borderWidth = 0.5
enableAllControls()
return true
} else {
errorLabel.hidden = false
textField.layer.borderColor = UIColor.redColor().CGColor
textField.layer.borderWidth = 1.0
if let unwrappedContainer = container {
textField.becomeFirstResponder()
disableAllControls()
unwrappedContainer.userInteractionEnabled = true
unwrappedContainer.alpha = 1.0
}
return false
}
}
以及禁用和启用控件:
override func disableAllControls() {
self.FirstNameSection.propertyValue.userInteractionEnabled = false
self.FirstNameSection.alpha = 0.3
self.LastNameSection.userInteractionEnabled = false
self.LastNameSection.alpha = 0.3
self.AddressSection.userInteractionEnabled = false
self.AddressSection.alpha = 0.3
self.EmailSection.userInteractionEnabled = false
self.EmailSection.alpha = 0.3
}
override func enableAllControls() {
self.FirstNameSection.propertyValue.userInteractionEnabled = true
self.FirstNameSection.alpha = 1.0
self.LastNameSection.userInteractionEnabled = true
self.LastNameSection.alpha = 1.0
self.AddressSection.userInteractionEnabled = true
self.AddressSection.alpha = 1.0
self.EmailSection.userInteractionEnabled = true
self.EmailSection.alpha = 1.0
}
答案 0 :(得分:1)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([textField.accessibilityIdentifier isEqualToString:@"ClassSize"]) {
if ([self isNumeric:string]) {
return YES;
} else {
return NO;
}
}
return YES;
}
基本上,如果用户试图输入不正确的数据,键盘就没有响应。
我使用这种技术来防止超出一定范围的数字,甚至在输入十六进制数字时限制所述用户我记得。他们只能在场上输入0-F。
答案 1 :(得分:0)
事实证明,只需几行代码即可实现所有这一切。为了支持验证,UITextFieldDelegate协议定义了以下方法:
textFieldShouldBeginEditing(_:)
textFieldDidBeginEditing(_:)
textFieldShouldEndEditing(_:)
textFieldDidEndEditing(_:)
(参见文件here)
问题中的代码可以简化为:
var shouldEndEdit: Bool = true
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
validator.validateField(textField) { error in
if error == nil {
self.shouldEndEdit = true
} else {
self.shouldEndEdit = false
}
}
return self.shouldEndEdit
}
加上here所述的验证规则。