ios - iOS 上的 UITextField 验证有什么更好的方法?
问题描述
我必须对库中使用的 ui 文本字段进行验证RSFloatInputView
。
这是我的xib
import UIKit
import RSFloatInputView
class TextInputLayout: UIView {
@IBOutlet weak var revealButton: UIButton!
@IBOutlet weak var warningLabel: UILabel!
@IBOutlet weak var rsFloatingView: RSFloatInputView!
var contentView: UIView?
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
func xibSetup() {
contentView = loadViewFromNib()
contentView!.frame = bounds
contentView!.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth, UIView.AutoresizingMask.flexibleHeight]
addSubview(contentView!)
}
func loadViewFromNib() -> UIView! {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "TextInputLayout", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
revealButton.tintColor = Color.grayColor()
warningLabel.textColor = UIColor.red
return view
}
}
我想在这个视图控制器中实现这个,当我点击下一步按钮时
import UIKit
import DLRadioButton
class SecureWalletViewController: UIViewController,UITextFieldDelegate {
@IBOutlet weak var securityPinStackView: UIStackView!
@IBOutlet weak var securityPin: TextInputLayout!
@IBOutlet weak var confirmSecurityPin: TextInputLayout!
@IBAction func onNextButtonTap(_ sender: Any) {
}
func textInputLayout(at index:Int) -> TextInputLayout {
return securityPinStackView.arrangedSubviews[index] as! TextInputLayout
}
}
解决方案
UITextFieldDelegate
对如下给出的方法使用验证:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return true
}
或者在这里使用自定义验证功能
推荐阅读
- wordpress - 向 WordPress 插件发送的电子邮件添加附件
- javascript - 过滤对象数组,其中对象中的一个字段是数组
- javascript - 验证表单是否已完全提交
- django - Django - 时间场差异
- php - 试图让 PHP 流与 Thawte TLS RSA CA G1 一起工作 - 内部错误
- visual-studio-code - 缺少导入时如何从 VS Code 创建文件
- javascript - 在某些拒绝案例中,我如何打破我的承诺链
- quartz.net - Quartz.net - 我应该手动删除安排为一次性执行的作业吗?
- python - 如何对同一文本样本中的不同主题/方面进行情感分析
- android - WearOS 中 SensorManager 的问题