【问题标题】:Swift Multiple Text Field ArgumentsSwift 多文本字段参数
【发布时间】:2017-08-25 08:50:39
【问题描述】:

我有多个限制要添加到 2 个单独的文本字段中。

文本字段1 , textField2

两个文本字段

  • 只允许一个小数

  • 只允许长度为 8 个字符

"shouldChangeCharactersIn" - 如何使用此函数应用于多个文本字段并具有多个约束?

func textField(_ textField: UITextField,shouldChangeCharactersIn range: NSRange,replacementString string: String) -> Bool
    {
    let countdots = (capitalInvested.text?.components(separatedBy: ".").count)! - 1

        if countdots > 0 && string == "."
        {
            return false
        }
        return true
        }

【问题讨论】:

  • 检查内部shouldChangeCharactersIn if (textField == textField1 || textField2) { // apply your both conditions}

标签: ios swift textfield


【解决方案1】:

您需要检查正在输入的文本字段,它调用了该函数:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField == textField1 {
        // first text field
        // add restrictions here
    } else if textField == textField2 {
        // second text field
        // add restrictions here
    }
    if textField == textField1 || textField == textField2 {
        // restrictions for both
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    相关资源
    最近更新 更多