gpt4 book ai didi

ios - iOS 中的 UITextField 允许单个数字

转载 作者:行者123 更新时间:2023-11-30 13:13:58 24 4
gpt4 key购买 nike

我有一个Verification ViewController,我通过短信获得4位验证码,我需要输入这些代码才能登录,我已经像这样创建了ViewController

正如您所看到的四个 UITextField,我只需要为每个 UITextField 允许使用一位数字,

我尝试过的:我尝试使用 shouldChangeCharactersInRange:method: ,但它没有被调用,我不知道出了什么问题,我认为是因为 UITextFields位于 UITableView 中,因此它不起作用。

最佳答案

您可以使用文本字段的委托(delegate)功能来更改文本字段。最初,您需要设置委托(delegate)和每个文本字段的标签。

- (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 - iOS 中的 UITextField 允许单个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38428927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com