检查 UITextField 是否有击键发生,这让我发疯:-)
我对此很陌生,很抱歉问这个问题。
我试图让它适用于我的代码,但就是不明白。无论我似乎做什么,我按下的键都会在一次击键后显示在 NSLog 中。我知道“shouldChangeCharactersInRange”是这样工作的,我已经浏览并尝试测试我找到的一些解决方案。一种是: 使用 `textField:shouldChangeCharactersInRange:`,如何我能得到包含当前输入字符的文本吗? 但我仍然不明白。
在我尝试让它工作并将其改回来之后,我当前的测试代码:
-(BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
NSLog(@"%@", MyTextField.text);
MyTextField.text = MyTextField.text;
return YES;
}
MyTextField 是我输入的文本字段。
有人可以告诉我如何完成这项工作,以便我可以跟踪每次击键发生时的情况,而不是之后的击键吗?
I am very new to this and i am sorry to ask this question.
I am trying to get this to work for my code but just don't get it. Whatever i seems to do the key that i press show up in the NSLog one key stroke after. I know that the 'shouldChangeCharactersInRange' works this way and i have surfed and tried to test some solutions i have found. One is: Using `textField:shouldChangeCharactersInRange:`, how do I get the text including the current typed character? but i still just don't get it.
my current test code after i have tried to get it to work and changed it back:
-(BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string {
NSLog(@"%@", MyTextField.text);
MyTextField.text = MyTextField.text;
return YES;
}
MyTextField is the textfield i type into.
Could someone nice show me how to get this work so i can track each keystroke when it happens and not the keystroke after?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要查看刚刚按下的键,请使用:
从技术上讲,这可能是多个字符,例如剪切、粘贴或撤消操作。
在该方法完成之前,文本字段的值实际上不会改变。如果您想查看文本字段的值是什么,请使用
To see the key that was just pressed, use:
Technically, this could be more than one character, like with a cut, paste, or undo operation.
The value of the text field doesn't actually change until after the method completes. If you want to see what the value of the text field will be, use