如何获取用户输入时 UITextField 中显示的实际字符串?
UITextFieldDelegate 协议仅提供一种合理的方法来获取有关 UITextField 中的更改的通知:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
此方法不提供像在 textField 中可见的实际字符串。它在 UITextField 中的文本属性更新为新文本之前被调用。 string
只是发生了变化的部分。
我想找出答案的唯一方法是根据这些信息手动组装文本。我该怎么办呢?我必须发明解决方案还是已经有了解决方案?
There is the UITextFieldDelegate protocol, which offers only one reasonable method to get notified about changes in the UITextField:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
This method doesn't provide the actual string like it is visible in the textField. It gets called before the text property in the UITextField updates to the new text. The string
is only what has changed.
I guess the only way to find out, is to assemble the text manually from this information. How would I go about this? Must I invent the solution or is there already one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能只是不需要委托方法。
如果您使用 IB,您可以创建一个像这样的 IBAction
-(IBAction)myTextFieldDidChange:(id)sender{
NSString*aString = myTextField.text;
}
只需将此函数连接到相应的 UITextField(在本例中为 myTextField)
对于事件 ValueChanged (UIControlEventValueChanged)
You just might not need a delegate method.
If you're using IB, you can create an IBAction like this
-(IBAction)myTextFieldDidChange:(id)sender{
NSString*aString = myTextField.text;
}
just connect this function to your corresponding UITextField(which in this case, myTextField)
for events ValueChanged (UIControlEventValueChanged)