-3
应用程序要求用户将4位数字输入到文本字段中。你怎么把它限制在4?以及如何限制到数字?Swift文本字段(IOS,Xcode 7.3,Swift 2):4位数字的用户密钥。如何将输入限制为数字并将数字位数限制为4
应用程序要求用户将4位数字输入到文本字段中。你怎么把它限制在4?以及如何限制到数字?Swift文本字段(IOS,Xcode 7.3,Swift 2):4位数字的用户密钥。如何将输入限制为数字并将数字位数限制为4
这里是一段代码 - >不要忘了设置代理上的UITextField
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let nonNumberSet = NSCharacterSet.decimalDigitCharacterSet().invertedSet
if (string.stringByTrimmingCharactersInSet(nonNumberSet).characters.count > 0 && textField.text?.characters.count < 4) || (string.characters.count == 0 && range.length > 0) {
return true
}
return false
}
https://www.google.co.uk/search?q=uitextfield+limit+input+stackoverflow +快捷 – Wain