简体   繁体   English

我怎样才能使具有焦点的NSTextField重绘呢?

[英]How can I make an NSTextField that has focus redraw itself?

I've subclassed and defined my own custom NSFormatter and NSTextField classes in order properly display SMPTE time code values ( HH:MM:SS:FF ). 我已经继承并定义了自己的自定义NSFormatter和NSTextField类,以便正确显示SMPTE时间码值( HH:MM:SS:FF )。 When the user selects to use drop-frame for the fps value, the format changes to HH:MM:SS;FF (semi-colon at the end). 当用户选择使用丢帧作为fps值时,格式更改为HH:MM:SS;FF (末尾为分号)。 I have this all working well except for one final hurdle. 除了最后的一个障碍,我的所有工作都很好。

When changing the format, the NSTextField with the focus does not update it's display until it's focus is lost. 更改格式时,具有焦点的NSTextField不会更新,直到失去焦点为止。 Here is a screenshot to illustrate: 这是说明的屏幕截图:

更改为丢帧格式

I have tried sending the NSTextField a setNeedsDisplay message when the format is changed, as well as sending it a display message. 我尝试过在格式更改时向NSTextField发送setNeedsDisplay消息,以及向其发送display消息。 Nothing seems to work so far, and the text field with focus refuses to update/redraw itself until it loses focus. 到目前为止,似乎没有任何工作,并且具有焦点的文本字段拒绝更新/重绘自身,直到失去焦点。

Any suggestions of what I need to do to make this happen? 我需要做些什么才能做到这一点?

I did manage to solve this problem now. 我确实设法解决了这个问题。 It may be a bit of a hack, but basically what I ended up doing was retrieving the NSTextField 's field editor with currentEditor and sending it as an object with the posted notification for when the format changes. 可能有点骇人听闻,但是我最终要做的基本上是使用currentEditor检索NSTextField的字段编辑器,并将其作为对象发送,并在格式更改时发布通知。

With the field editor I then just had it replaceCharactersInRange:withString: to change the last ":" to ";" 然后,使用字段编辑器将它replaceCharactersInRange:withString:replaceCharactersInRange:withString:将最后一个“:”更改为“;”。 and vice-versa. 反之亦然。 Though it may not be optimal, it works perfectly. 尽管它可能不是最佳选择,但它可以完美运行。

In fact, I've posted the solution to github for anyone else who might find this useful. 实际上,我已将解决方案发布给github,以供其他可能觉得有用的人使用。 https://github.com/cfloisand/smpteformatter https://github.com/cfloisand/smpteformatter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM