如何以编程方式打开 NSComboBox 列表?
我已经解决这个问题有一段时间了..我认为这应该是一个简单的任务,但它不是 =D
我想做的是,当用户单击组合框时显示组合框的列表,但不是专门在按钮。
有什么想法吗? 提前致谢!
I've been around this for a while.. I thought this should be an easy task, but it isn't =D
What I am trying to do, is to display the combobox's list when the user clicks the combobox but not specifically in the button.
Any Idea?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用以下代码行:
You can use the following code line:
放入
就是
我最终得到的。谢谢@Ahmed Lotfy
这是完整的代码,它在 OSX 10.11 上适用于我
Put
Into
is what I ended up with. Thanks @Ahmed Lotfy
Here's the full code, it works for me on OSX 10.11
感谢上面提到的 jmoody 和 Jens Alfke。以下是上述解决方案的 SWIFT 翻译。
CComboBoxEx 类:NSComboBox
{Thanks to jmoody and Jens Alfke mentioned above. Here is a SWIFT translation of the above solution.
class CComboBoxEx: NSComboBox
{NSComboBox 的设计初衷并不是以这种方式工作。由于用户可能想要编辑控件中的文本,因此他们需要能够单击它而不会意外弹出选项。
您需要子类化 NSComboBoxCell 并更改此行为...但是您将拥有一个看起来标准的控件,但其行为却不是标准方式。如果您决心这样做,请查看 NSComboBoxCell。有趣的方法似乎是 -popUpForComboBoxCell: 和朋友。
NSComboBox was not designed to work this way. Because the user may want to edit the text in the control, they'll need to be able to click it without unexpectedly popping up the choices.
You would need to subclass NSComboBoxCell and change this behavior ... but then you'd have a standard-looking control that does not behave in a standard way. If you're determined to do this, take a look at the open source version of NSComboBoxCell. The interesting methods appear to be -popUpForComboBoxCell: and friends.
基于其他答案,我编写了这个解决方案(使用 Xcode 10.2.1、Swift 5 进行测试)。它使用相同的想法,但更短一些。
Based on the other answers I wrote this solution (tested with Xcode 10.2.1, Swift 5). It uses the same ideas but it's a little shorter.
这个答案符合问题的标题,但不符合问题本身。奥马尔想要触摸文本字段并弹出框。
该解决方案在用户输入文本时显示弹出窗口。
我在 Jens Alfke 的 cocoabuilder 上找到了这个答案。我在这里重新发布了他的代码。谢谢詹斯。
原始 cocoabuilder 帖子:(http ://www.cocoabuilder.com/archive/cocoa)
我在我的
controlTextDidChange:
方法中使用了此代码。This answer fits the title of the question, but not question itself. Omer wanted to touch a text field and have the box popup.
This solution shows the popup when the user enters text.
I found this answer on cocoabuilder from Jens Alfke. I reposted his code here. Thanks Jens.
original cocoabuilder post: (http://www.cocoabuilder.com/archive/cocoa)
I used this code in my
controlTextDidChange:
method.如果 NSComboBox 的列表已展开,则返回
true
打开 NSComboBox 的列表
关闭 NSComboBox 的列表
Ref. jmoody 的回答。
Returns
true
if the NSComboBox's list is expandedOpen the NSComboBox's list
Close the NSComboBox's list
Ref. jmoody’s answer.