如何在 UITextField 中显示用户输入的字符之前访问它

发布于 2024-12-08 10:12:17 字数 532 浏览 0 评论 0原文

我正在编写应用程序,其中我需要使用某种方法逐个字符地验证用户输入到 UITextField 的文本。

困难的是客户端希望在用户看到 UITextField 中的字符之前完成所有验证,因为可能存在他的服务器应用程序不支持 '$' 符号的情况,所以在我的验证方法中我应该将其替换为 'USD' 字符串 - 并且他不希望用户立即看到 '$',而只看到 'USD'

我知道诸如 UIControlEventEditingChanged 等事件,但我仍然不知道两件事:

  • 如何在 UITextField 中看到用户输入的字符之前访问它在那里执行验证

  • 如何“即时”替换该字符并将其手动放置到UITextField (但我想我只是将其附加到 [[textField] text] NSString

提前感谢您的帮助:)

I'm writing application in which I need to validate text entered by user to UITextField, char by char with some method.

The difficult thing is that client wants to do all the validation before user see character in the UITextField because there might be situation that his server application doesn't support '$' sign, so in my validation method I should replace it with 'USD' string - and he doesn't want user to see '$', just 'USD' immediately.

I know about events like UIControlEventEditingChanged etc., but still, I don't know 2 things:

  • how to access character typed by user before it's seen in UITextField and execute validation there

  • how to subtitute this character 'on the fly' and put it manually to UITextField (but I suppose I'll just append this to [[textField] text] NSString

Thank You in advance for any help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反差帅 2024-12-15 10:12:17

实现 UITextFieldDelegate 方法 textField:shouldChangeCharactersInRange:replacementString:,例如:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    BOOL validated = ...; //do your validation
    return validated;
}

Implement the UITextFieldDelegate method textField:shouldChangeCharactersInRange:replacementString:, e.g.:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    BOOL validated = ...; //do your validation
    return validated;
}
迟月 2024-12-15 10:12:17

类似于 omz 的答案,但如果您需要更完整的代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSString *filtered;
    filtered = [string stringByReplacingOccurrencesOfString:@"$" withString:@"USD"];

    //optionally if you want to only have alphanumeric characters
    //NSMutableCharacterSet *mcs1 = [[[NSCharacterSet letterCharacterSet] invertedSet] mutableCopy];  //only alphabet character
    //[mcs1 removeCharactersInString:@"0123456789"];  
    //filtered = [[filtered componentsSeparatedByCharactersInSet:mcs1] componentsJoinedByString:@""];
    //release mcs1;

    return [string isEqualToString:filtered];
}

Similar to omz's answer but more complete code if you need:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSString *filtered;
    filtered = [string stringByReplacingOccurrencesOfString:@"$" withString:@"USD"];

    //optionally if you want to only have alphanumeric characters
    //NSMutableCharacterSet *mcs1 = [[[NSCharacterSet letterCharacterSet] invertedSet] mutableCopy];  //only alphabet character
    //[mcs1 removeCharactersInString:@"0123456789"];  
    //filtered = [[filtered componentsSeparatedByCharactersInSet:mcs1] componentsJoinedByString:@""];
    //release mcs1;

    return [string isEqualToString:filtered];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文