按下标准数字键盘时清除 UITextField
如果我没有在标题中充分解释我的问题,我很抱歉。
在我的应用程序中,我使用一个小子视图在屏幕顶部创建一个非常基本的计算器(我只有一个 UITextField 来显示操作和两个按钮)。我想使用标准的 iPhone 数字键盘,而不是使用自定义键盘。
我的问题是,在执行操作(例如添加两个数字)并显示结果后,我无法弄清楚当用户输入新数字时如何清除屏幕。
因此,例如:
- 用户输入“6”->数字 6 显示在 UITextField 中
- 用户选择“+”-> UITextField 被清除,为下一个数字腾出空间
- 用户输入“10” ->数字 10 显示在 UITextField 中
用户选择“+”-> 16 显示为先前操作的结果,并且应该保留在那里直到按下另一个数字(他想继续添加更多数字)
用户输入“5”
此时,如果我使用自定义键盘,一旦用户按下按钮“5”,我就可以清除 UITextField,但在使用标准数字键盘时我不知道如何执行此操作。所以,我现在得到的结果是“165”。
有没有办法检测标准数字键盘中何时按下按键,以便我可以在新数字出现之前清除 UITextField?我认为可能有一个 NSNotification,但我找不到它。
我知道如果我创建一个自定义键盘或者使用两个单独的 UITextFields(一个用于操作,另一个用于总数),我可以解决我的问题,但如果可能的话,我想使用标准数字键盘。
非常感谢!
I'm sorry if I didn't explain my problem well enough in the title.
In my application, I'm using a small subview to create a very basic calculator on the top side of the screen (I just have an UITextField to show the operations and two buttons). Instead of using a custom keyboard for it, I want to use the standard iPhone number pad.
My problem is that after doing an operation (e.g. adding two numbers) and showing the result, I cannot figure out how to clear the screen when the user enters a new number.
So, for example:
- User enters "6" -> Number 6 is shown in UITextField
- User selects "+" -> UITextField is cleared to make room for the next number
- User enters "10" -> Number 10 is shown in UITextField
User selects "+" -> 16 is shown as the result of the previous operation, and should stay there until another number is pressed (he wants to continue adding more numbers)
User enters "5"
At this point, if I was using a custom keyboard, I could clear the UITextField as soon as the button "5" is pressed by the user, but I cannot figure out how to do so when using the standard number pad. So, the result I get at the moment is "165".
Is there a way to detect when a key is pressed in the standard number pad so that I can clear the UITextField before the new number appears? I thought there may be a NSNotification for that, but I couldn't find it.
I'm aware that I could solve my problem if I created a custom keyboard or if I used two separated UITextFields (one for the operations and another one for the total), but I would like to use the standard number pad if it's possible.
Thanks very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为 UITextField 设置委托并使用 shouldChangeCharactersInRange:replacementString:
这将捕获每个单独的击键,使您能够执行您想要的操作。请参阅
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textField:shouldChangeCharactersInRange:replacementString :
如果我理解正确的话,应该涵盖它。
Set a delegate for the UITextField and use shouldChangeCharactersInRange:replacementString:
This will trap every individual keystroke enabling you to do what you want. See
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textField:shouldChangeCharactersInRange:replacementString:
If I've understood you correctly that should cover it.