我输入的密码为UITextField
我的要求是当textField字符范围达到第6个字符然后我进行服务器验证时我必须将数据放入标签中。如果字符范围小于5,那么我将传递nil值。
以下是我的代码: -
//TextField character range
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == pinCode
{
guard let text = textField.text else { return true }
let newLength = text.characters.count + string.characters.count - range.length
if(5 >= newLength)
{
print(newLength)
}
else
{
if newLength == 6
{
serverValidation()
print(textField.text!)
setFields(city: "Ajmer", state: "Rajasthan", country: "India")
return newLength <= 6
}
else
{
return newLength <= 6
}
}
}
return true
}
我的问题是什么时候(newLength == 6)然后我只得到5个字符的textField文本。我的意思是如果假设邮政编码是560068那么文字我只得到56006。 (但我在这种情况下只对6个字符的邮政编码进行服务器验证)
所以,如果(newLength == 6)那么如何在文本字段中获得6个字符,然后我只能在这里进行服务器验证。
我是斯威夫特的新人。
由于
答案 0 :(得分:3)
shouldChangeCharactersIn
中的实际文本之前,会先调用 UITextField
这是您从UITextField
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
修改强>
这就是你如何使用它
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == pinCode
{
guard let text = textField.text else { return true }
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
let newLength = newString.characters.count
if(5 >= newLength)
{
print(newLength)
}
else
{
if newLength == 6
{
serverValidation()
print(newString)
setFields(city: "Ajmer", state: "Rajasthan", country: "India")
return newLength <= 6
}
else
{
return newLength <= 6
}
}
}
return true
}
答案 1 :(得分:1)
使用 TextField 委托方法。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentCharacterCount = textField.text?.characters.count ?? 0
if (range.length + range.location > currentCharacterCount+1)
{
return false
}
print(currentCharacterCount)
let newLength = currentCharacterCount + string.characters.count - range.length
return newLength <= 6 //this will not allow you to enter more than 6 characters
}
之后的电话
func textFieldDidEndEditing(_ textField: UITextField)
{
print(textField.text!)
setFields(city: "Ajmer", state: "Rajasthan", country: "India")
}
要调用此方法,您可以使用view.endEditing(true)
方法。
答案 2 :(得分:1)
您应该从08 35 // 1: [u]int32, = 53
15 60485958 // 2: fixed32, = 1615354200
18 40 // 3: [u]int32, = 64
22 0a 0d6048595815bdf42642 // 4: sub-message, length 10
0d 60485958 // 1: fixed32, = 1615354200
15 bdf42642 // 2: fixed32, = 3186894402
2a 0a 0d7047595815bdf426 // 5: sub-message, length 10, incomplete
0d 70475958 // 1: fixed32, = 1883724120
15 bdf426 // 2: fixed32, incomplete
+ newlength
textfield.text.length
然后从string.length
+ textfield.text
获取文字。
在调用return true之前,您无法从string
获取新文本。
答案 3 :(得分:0)
试试这个
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == pinCode
{
guard let text = textField.text else { return true }
var txtAfterUpdate:NSString = textField.text! as NSString
txtAfterUpdate = txtAfterUpdate.stringByReplacingCharactersInRange(range, withString: string)
let txtString = txtAfterUpdate as String
let newLength = txtString.characters.count
if(5 >= newLength)
{
print(newLength)
}
else
{
if newLength == 6
{
print(textField.text!)
setFields(city: "Ajmer", state: "Rajasthan", country: "India")
return newLength <= 6
}
else
{
return newLength <= 6
}
}
}
return true
}