尽管有 ShouldChangeCharactersIn 函数,但 textField 允许所有字符

textField allowing all characters despite ShouldChangeCharactersIn function

因为我试图内化我多年来一直使用的代码(没有太多理解),所以我创建了我的版本,理论上 应该复制它的目的。 我有一个 textField,其中我只允许使用十进制数字和一个句点 - “.”。但是,目前,我的 textField 允许输入任何字符。

我导入了 UITextFieldDelegate class,将我的 UITextField 连接为插座,并将我的文本字段设置为 viewDidLoad 中的 textFieldDelefate。

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

   //characterSet that holds digits
var allowed = CharacterSet.decimalDigits

//initialize the period for use as a characterSet
let period = CharacterSet.init(charactersIn: ".")

//adding these two characterSets together
allowed.formUnion(period)

//all the characters not in the allowed union
let inverted = allowed.inverted

//if latest input is from the characters not allowed is present (aka, empty), do not change the characters in the text range
if string.rangeOfCharacter(from: inverted) != nil
{
    return false
}
    //if the text already contains a period and the string contains one as well, do not change output
else if (textField.text?.contains("."))! && string.contains(".")
{
    return false
}
//however, if not in the inverted set, allow the string to replace latest value in the text
else
{
    return true
}

该功能没有禁用多句号和小数倒数。

我似乎为我工作,我将一些角色设置移到了一些惰性变量中,这样它只完成一次,而不是每次调用委托时。

import UIKit

class ContainerController: UIViewController, UITextFieldDelegate {


    @IBOutlet weak var textField: UITextField!

    //characterSet that holds digits
    lazy var allowed:CharacterSet = {
        var allowed = CharacterSet.decimalDigits
        //initialize the period for use as a characterSet
        let period = CharacterSet.init(charactersIn: ".")
        //adding these two characterSets together
        allowed.formUnion(period)
        return allowed
    }()

    lazy var inverted:CharacterSet = {
        return allowed.inverted
    }()

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        textField.delegate = self
    }


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

        print("shouldChangeCharactersIn", range)
        print("replacementString", string)
        //if latest input is from the characters not allowed is present (aka, empty), do not change the characters in the text range
        if string.rangeOfCharacter(from: inverted) != nil {
            return false
        } else if (textField.text?.contains("."))! && string.contains(".") {
            //if the text already contains a period and the string contains one as well, do not change output
            return false
        } else {
             //however, if not in the inverted set, allow the string to replace latest value in the text
            return true
        }
    }

}