问题描述
我有一个验证ViewController
,我通过短信获得4位数的验证码,我需要输入这些代码才能登录,我创建了 ViewController
喜欢这个
I have a Verification ViewController
, I get 4 digit verification code by SMS and I need to enter those code to login, I have created the ViewController
like this
正如你可以看到四个 UITextField
,我需要只允许每个<$ c $的一位数c> UITextField ,
As you can see four UITextField
s, I need to allow only single digit for each UITextField
,
我尝试了什么:我试图使用 shouldChangeCharactersInRange:method:
,但它没有被调用,我不知道出了什么问题,我想因为 UITextField
s在 UITableView
所以它不起作用。
What I tried: I was trying to use shouldChangeCharactersInRange:method:
, but its not getting called, I don't know what's wrong, I think because UITextField
s are in UITableView
so it is not working.
推荐答案
您可以使用文本字段的委托功能更改这样的文本字段。最初,您需要设置每个文本字段的委托和标记。
You can change the text field like this by using the delegate function of the text field. Initially, you need to set the delegate and the tag of each text field.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ((textField.text.length >= 1) && (string.length > 0))
{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (! nextResponder)
nextResponder = [textField.superview viewWithTag:1];
if (nextResponder)
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
return NO;
}
return YES;
}
Swift 2
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// On inputing value to textfield
if (textField.text?.characters.count < 1 && string.characters.count > 0){
let nextTag = textField.tag + 1;
// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag);
if (nextResponder == nil){
nextResponder = textField.superview?.viewWithTag(1);
}
textField.text = string;
nextResponder?.becomeFirstResponder();
return false;
}
else if (textField.text?.characters.count >= 1 && string.characters.count == 0){
// on deleting value from Textfield
let previousTag = textField.tag - 1;
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag);
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1);
}
textField.text = "";
previousResponder?.becomeFirstResponder();
return false;
}
return true;
}
Swift 4
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.text!.count < 1 && string.count > 0{
let nextTag = textField.tag + 1
// get next responder
var nextResponder = textField.superview?.viewWithTag(nextTag)
if (nextResponder == nil){
nextResponder = textField.superview?.viewWithTag(1)
}
textField.text = string
nextResponder?.becomeFirstResponder()
return false
}
else if textField.text!.count >= 1 && string.count == 0{
// on deleting value from Textfield
let previousTag = textField.tag - 1
// get next responder
var previousResponder = textField.superview?.viewWithTag(previousTag)
if (previousResponder == nil){
previousResponder = textField.superview?.viewWithTag(1)
}
textField.text = ""
previousResponder?.becomeFirstResponder()
return false
}
return true
}
这篇关于允许在iOS中的UITextField中使用单个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!