2

我有一个 UITextField,其下方有一个表格,显示了可供选择的项目列表(类似于下拉列表)。现在,例如,当我在文本字段中键入 2(文本字段具有年份值)时,该表将显示所有带有 2 的字符串作为子字符串。所以当我输入 2000 时,它只会在表中显示匹配的字符串 2000。

现在,当我在文本字段中完成输入 2000 时,我想调用一个方法。一切正常,但我只想在我完成输入所有 4 位数字时调用此方法,但是当我尝试输入第 4 位数字时调用此方法。

我如何在输入 2000 的地方执行此操作,并且在输入第三个零后,它会在 shouldChangeCharactersInRange 返回 Yes 后调用该方法。

这是我的代码:

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

    //if _filteredarray count==1 and substring and _filteredarray object at index 0 matches then call a method here

     return YES;
}
4

1 回答 1

3

试试这个

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
     NSString *substring = [NSString stringWithString:textField.text];
     substring = [substring stringByReplacingCharactersInRange:range withString:string];
     if(substring.length == 4)
     {
          [textField resignFirstresponder];
          [self performSelector:@selector(functiontocall)withObject:nil afterDelay:0.8];
     }
     return YES;
}

上面的代码可能会给你一个想法

如果你输入 2000 的第四个字母,那么键盘会消失(如果你想你可以添加它,这样可以避免在文本字段中进一步输入值),然后你可以看到第三个零 0.8 秒,你需要调用的函数将叫做。

于 2013-03-07T09:32:22.497 回答