ios - Swift 多文本字段参数

标签 ios swift textfield

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

文本字段1 , 文本域2

两个文本字段

  • 只允许一位小数

  • 长度只允许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
        }

最佳答案

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

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
    }
}

关于ios - Swift 多文本字段参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45877709/

相关文章:

ios - 通过 CocoaPods 添加了适用于 iOS 的 Google map 的 Swift 项目中的链接器错误

ios - UIPickerView - 用于计算的值传递

iphone - 调整 tableView 边界以显示单个部分是否合法?

ios - 快速在 TableView 的最后一个元素下方添加空间

swift - 当 UIView 展开时,如何向下移动文本字段(Swift)

ios - 密码字段的键盘仅在 iOS 12 上从 azerty 切换到 qwerty(有时)

java - 保存 TextField 值时出现 "java.lang.NullPointerException"- JavaFx

ios - 嵌入 UIViewController 的子容器的自动布局

macos - 是否有类似 NSFileManager.enumeratorAtPath 的东西需要从递归中排除的目录列表?

swift - tiffRepresentation 和 bitmapRepresentation 在 Base64 字符串编码方面的区别?