子类化 NSTextField 以创建号码拨号文本字段
我正在尝试创建一个号码拨号文本字段,即一个与旁边的 NSStepper 结合使用的文本字段。
文本字段应显示浮动并允许用户使用滚轮来调整其显示的数字以及向上和向下箭头键以达到相同的效果。
它还应该根据按下的修改键切换到不同的增量。 稍后我可能会实现一种行为,当用户在按下 alt 的同时将鼠标拖动到左侧或右侧的文本字段上时,它将确定插入插入符的位置(数字的哪位),并且仅增加该数字,以便用户可以快速更新个位、十位、百位等部分。
虽然修改键和鼠标拖动细节只是细节,但我的问题更关心以最不引人注目的方式实现这一点的基本原理,以便绑定和其他功能仍然继续工作。
到目前为止,我得到了 NSTextField 的子类(在 IB 中的单元格上附加了一个数字格式化程序)和 NSTextView 的子类,作为号码拨号文本字段的字段编辑器。这样我就可以覆盖 keyDown: 以使箭头键发生更新。
问题是,从窗口委托的 windowWillReturnFieldEditor:toObject: 返回号码拨号文本字段的字段编辑器似乎破坏了绑定,并且通常感觉我不应该这样做。
我是否以错误的方式处理这个问题?
PS:您可以在 http://gist.github.com/361265 找到子类代码 我不确定是否应该将其全部发布在这里。
I am trying to create a number dialing text field, that is, a text field that works in conjunction with an NSStepper sitting next to it.
The text field should display floats and allow the user to use the scroll wheel to adjust the number it is displaying as well as the up and down arrow keys to the same effect.
It should also switch to different increments depending on what modifier keys are pressed.
Later on I might implement a behaviour where when the user drags the mouse over the text field left or right while pressing alt it will determine where (at what digit of the number) the insertion caret was placed and only increment that number so that the user can quickly update the ones, tens, hundreds, etc. parts.
While the modifier keys and mouse dragging details are rather just that, details, my question is more concerned with the fundamentals of achieving this in a most unobtrusive manner, so that bindings and other features still continue to work.
So far I got a subclass of NSTextField (with a number formatter attached to its cell in IB) and a subclass of NSTextView for being the number dialing text field's field editor. This is so that I can override keyDown: to make the arrow keys updates happening.
The problem is that returning this field editor for the number dialing text field from windowWillReturnFieldEditor:toObject: of the window's delegate seems to break bindings and generally feels like I shouldn't be doing this.
Am I going about this the wrong way?
PS.: You can find just the subclass code at http://gist.github.com/361265
I wasn't sure if I should post it all here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我过去用过这个。首先将号码格式化为 tel: URL 格式,如下所示:
然后我做了什么(因为我的实现涉及静态 UITableView,我检查了用户触摸的行并执行了以下操作:
您可以通过多种方式实现此目的,但使用 tel :格式化然后调用 OpenUrl 方法对我来说非常有效。
I have used this in the past. First format the number as with the tel: URL format like so:
Then what I did (since my implementation involved a static UITableView, I checked the row that the user touched and did this:
You can implement this several ways, but using the tel: format and then calling the OpenUrl method worked really well for me.