gpt4 book ai didi

iOS:shouldChangeCharactersInRange 返回旧结果

转载 作者:行者123 更新时间:2023-11-28 22:14:11 26 4
gpt4 key购买 nike

我有一个简单的 UItextField。在这一个我想只有 4 个数字(字符串)。如果此字符串不等于另一个字符串,则应清除输入(完美运行)。但在 shouldChangeCharactersInRange 中,最后一个数字仍然存在,代表新字符串中的第一个数字。

如何清除所有内容并将 shouldChangeCharactersInRange 设置为基本位置?

代码

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == passcodeTextfield)
{
NSString *passcode = [textField.text stringByReplacingCharactersInRange:range withString:string];

if (passcode.length == 0)
{
firstPasscodeDigitTextfield.text = @"";
}
else if (passcode.length == 1)
{
firstPasscodeDigitTextfield.text = @"\u25cf";
secondPasscodeDigitTextfield.text = @"";
}
else if (passcode.length == 2)
{
secondPasscodeDigitTextfield.text = @"\u25cf";
thirdPasscodeDigitTextfield.text = @"";
}
else if (passcode.length == 3)
{
thirdPasscodeDigitTextfield.text = @"\u25cf";
fourthPasscodeDigitTextfield.text = @"";
}
else if (passcode.length == 4)
{
fourthPasscodeDigitTextfield.text = @"\u25cf";

[self verifyPasscode:passcode];
}
else if (passcode.length > 4)
{
return NO;
}
}

return YES;
}

- (void)verifyPasscode:(NSString *)passcode
{
if ([passcode isEqualToString:@"4277"])
{
[passcodeTextfield resignFirstResponder];
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
[passcodeTextfield resignFirstResponder];
[passcodeTextfield becomeFirstResponder];
[self textFieldShouldClear:passcodeTextfield];
}
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField == passcodeTextfield)
{
passcodeTextfield.text = @"";
firstPasscodeDigitTextfield.text = @"";
secondPasscodeDigitTextfield.text = @"";
thirdPasscodeDigitTextfield.text = @"";
fourthPasscodeDigitTextfield.text = @"";
}

return YES;
}

最佳答案

我认为你应该这样做:

1) 当您为点(firstPasscodeDigitTextfield 等)创建 View 时,为它们设置标签:0,1,2,3

2) 当她更改时从 passwordTextField 中获取整个字符串值

3) 获取字符串的长度

4)实现功能:

-(void)setNpointsToView:(NSUInteger)n
{
//getting your views and set point to them
for (int i = 0; i < n; ++i)
{
[[self.view viewWithTag:i] setText: @"\u25cf"];
}
//set "" to other views
for (int i = n; i < 3; ++i)
{
[[self.view viewWithTag:i] setText" @""];
}
}

5) 每次使用 ((字符串的长度) - 1) 作为参数更改 passwordTextField 中的文本时调用此函数

6) 实现:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == passcodeTextfield)
{
NSString *passcode = [textField.text stringByReplacingCharactersInRange:range withString:string];

if (passcode.length > 3)
return NO;

}
}

关于iOS:shouldChangeCharactersInRange 返回旧结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22157902/

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