在菜单视图中以编程方式选择文本
我编写了一个“代理”Cocoa 应用程序,其中状态图标的菜单中有 TextField,它看起来像这样:
(来源:ifotos.pl)
并在有几个地方我以编程方式选择其内容(例如在我的 NSTextField 子类的 -(BOOL)becomeFirstResponder 中)
并且它不起作用。这肯定与它在菜单中而不是在窗口中这一事实有关。但我该如何解决这个问题呢?
I write an "Agent" Cocoa app where I have TextField in status icon's menu, it looks like this:
(source: ifotos.pl)
And in couple of places I select its contents programmatically (e.g. in -(BOOL)becomeFirstResponder of my NSTextField subclass)
And it doesn't work. It surely has something to do with the fact that it's in menu, not in window. But how do I fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您的视图位于菜单中,因此文本字段可能没有响应,因为运行循环未处于默认模式。尝试像这样调用
selectText:
:Because your view is in a menu, it's possible that the textfield isn't responding because the run loop is not in its default mode. Try calling
selectText:
like this:为什么不直接使用窗口呢?菜单在幕后被实现为窗口:您可以做同样的事情,只需适当地定位窗口并设置窗口样式即可。
编辑:答案大部分重写
Why don't you just use a window instead? Menus are implemented as windows under the hood: you can do the same thing, just position and style your window appropriately.
Edit: answer largely rewritten