为什么要替换更改文本字段中文本所需的字符?

时间:2017-09-26 15:39:23

标签: ios swift xcode textfield

我可以根据我在文本字段中输入的内容,使用以下代码行改变我在文本字段中显示数字的方式。但我甚至无法在我的代码的第二个版本中输入数字。

为什么

[let oldText = textField.text! as NSString
var newText = oldText.replacingCharacters(in: range, with: string)]

是必要的吗?

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

    let oldText = textField.text! as NSString
    var newText = oldText.replacingCharacters(in: range, with: string)

    var newTextString = String(newText)

    let digits = CharacterSet.decimalDigits
    var digitText = ""
    for c in (newTextString?.unicodeScalars)! {
        if digits.contains(UnicodeScalar(c.value)!) {
            digitText.append("\(c)")
        }
    }

    // Format the new string
    if let numOfPennies = Int(digitText) {
        newText = "$" + self.dollarStringFromInt(numOfPennies) + "." + self.centsStringFromInt(numOfPennies)
    } else {
        newText = "$0.00"
    }

    textField.text = newText

    return false
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    if textField.text!.isEmpty {
        textField.text = "$0.00"
    }
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()

    return true;
}

func dollarStringFromInt(_ value: Int) -> String {
    return String(value / 100)
}

func centsStringFromInt(_ value: Int) -> String {

    let cents = value % 100
    var centsString = String(cents)

    if cents < 10 {
        centsString = "0" + centsString
    }

    return centsString
}

如果我这样改变它,它就不再起作用了。

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

    var newText = textField.text


    let digits = CharacterSet.decimalDigits
    var digitText = ""
    for c in (newText?.unicodeScalars)! {
        if digits.contains(UnicodeScalar(c.value)!) {
            digitText.append("\(c)")
        }
    }

    // Format the new string
    if let numOfPennies = Int(digitText) {
        newText = "$" + self.dollarStringFromInt(numOfPennies) + "." + self.centsStringFromInt(numOfPennies)
    } else {
        newText = "$0.00"
    }

    textField.text = newText

    return false
}

3 个答案:

答案 0 :(得分:0)

textField(_:shouldChangeCharactersIn:replacementString:)在之前被称为,文本字段内的文本被替换。此方法用于在使用textField.text访问新文本之前获取新文本。

如果您只想在更换发生后运行逻辑,请观察名为.UITextFieldTextDidChange的通知。

NotificationCenter.default.addObserver(self, selector: #selector(textFieldTextDidChange), name: .UITextFieldTextDidChange, object: textField)

func textFieldTextDidChange() {
    print(textField.text)
}

答案 1 :(得分:0)

在修改文本字段之前调用

“shouldChangeCharactersIn”。由于您的代码始终返回false,因此无论用户输入(或粘贴)的内容都不会更新字段的内容,除非您自己执行此操作。

在第二个版本中,您完全忽略了用户的输入,因此您的字段值永远不会改变。

答案 2 :(得分:0)

让我们逐一回答。

1)不需要replacingCharacters(in: range, with: string)方法。此方法将替换与指定范围匹配的子字符串。所以,根据Apple的文档:

  

<强> stringByReplacingCharactersInRange:withString:

     

返回一个新字符串,其中接收器指定范围内的字符由给定字符串

替换

2)代码更改不起作用的真正原因是返回值false。尝试返回true,此更改将起作用。每当您设置文本字段text属性时,将调用方法textField:shouldChangeCharactersIn:replacementString:(因为它的委托已分配)。返回false您告诉程序它不应该替换字符。

<强>更新

尝试使用UIControl事件Value Changed作为@IBAction而不是textField:shouldChangeCharactersIn:replacementString:,这将很有效。

Obs:对不起我的英文