首页 > 解决方案 > 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
    } 
}

标签: iosswift

解决方案


UITextFieldDelegate对如下给出的方法使用验证:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return true
    }

或者在这里使用自定义验证功能


推荐阅读