简体   繁体   English

NSMenuItem的自定义视图中的可编辑NSTextField

[英]Editable NSTextField in custom view of NSMenuItem

I have a subclassed NSView that I would like to contain an editable NSTextField, similar to Spotlight. 我有一个子类NSView,我想包含一个可编辑的NSTextField,类似于Spotlight。

I am having significant difficult getting the text field to select and highlight. 我很难让文本字段选择并突出显示。 If I click quickly and rapidly over the editing area of the textfield, I can actually enter text, but it appears greyed out, as though I am adding text to a field of an app that is not foremost. 如果我快速快速地在文本字段的编辑区域上单击,我实际上可以输入文本,但它看起来是灰色的,就好像我正在将文本添加到不是最重要的应用程序的字段中。

I have tried to get focus of the View's window using viewDidMoveToWindow , but apparently NSMenuItem's windows are carbon windows that don't handle keys very well. 我试图使用viewDidMoveToWindow获得View窗口的viewDidMoveToWindow ,但显然NSMenuItem的窗口是碳窗口,不能很好地处理键。

Long story short, what is a good way to go about making this work successfully? 长话短说,成功完成这项工作的好方法是什么? I know that Spotlight is a borderless window, but that is not suitable for my purposes. 我知道Spotlight是一个无边框窗口,但这不适合我的目的。

Any and all suggestions would be very helpful. 任何和所有建议都会非常有帮助。

After much research, two things are common consensus: 经过大量研究,有两件事是共识:

(1) If you find yourself looking for ways to subclass a built-in UI element for special the chances are that you're doing something wrong, or at very least, there is a better way to do what you're trying to do. (1)如果你发现自己正在寻找将内置UI元素子类化为特殊元素的方法,那么你可能会做错事,或者至少有一种更好的方法可以做你想做的事情。 。

(2) The reason to follow the interface guidelines is to be more future-compatible. (2)遵循接口指南的原因是更具未来兼容性。 There is no guarantee that a sub-classed UI element will be compatible with future versions of the OS. 无法保证子类UI元素与操作系统的未来版本兼容。

Brass Tacks - subclassing UI elements is generally not advised. Brass Tacks - 通常不建议继承UI元素。

You might want to check out Matt Gemmell's MAAttachedWindow. 你可能想看看Matt Gemmell的MAAttachedWindow。

http://mattgemmell.com/2007/10/03/maattachedwindow-nswindow-subclass http://mattgemmell.com/2007/10/03/maattachedwindow-nswindow-subclass

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

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