Ios 需要检查我的UITextField中的实时输入
我有一个Ios 需要检查我的UITextField中的实时输入,ios,objective-c,uitextfield,uitextfielddelegate,Ios,Objective C,Uitextfield,Uitextfielddelegate,我有一个signUpViewController,允许用户使用用户名和密码注册。 我需要能够实时检查用户添加到usernameTextField的输入,我目前正在通过实现以下委托方法检查其输入: - (void)textFieldDidEndEditing:(UITextField *)textField { } 但是,这只会在用户离开文本字段后检查,然后我可以与我的服务器检查用户名是否已经存在。这并没有给我想要的行为,我想让用户立即知道他们正在键入的名称是否可用(这是用户对其他应用的期望)
signUpViewController
,允许用户使用用户名和密码注册。
我需要能够实时检查用户添加到usernameTextField
的输入,我目前正在通过实现以下委托方法检查其输入:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
}
但是,这只会在用户离开文本字段后检查,然后我可以与我的服务器检查用户名是否已经存在。这并没有给我想要的行为,我想让用户立即知道他们正在键入的名称是否可用(这是用户对其他应用的期望)
我已经查看了
UITextField
文档,但找不到一个可以完成我正在寻找的工作的委托方法,关于如何获得所需行为的任何想法?UITextField委托人是
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
在这种情况下,您可以使用
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
查看
UITextFieldDelegate
的文档并进行一些搜索。关于在键入时验证用户输入,有无数的讨论。我有@rmaddy,但我找不到我要查找的内容。我实现了-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
虽然我可以看到代码在调试器中工作,但它不允许我在文本字段中键入文本,知道为什么吗?委托方法没有涵盖UITextField的目标/操作功能。您最好先阅读apple的类参考。如果委托方法返回YES,它将更改textfield的文本,否则不会。