我有多个限制,我想添加到 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/