如何在节时将动作从NSCombobox发送到功能?
我正在以编程方式创建一个组合框,我想从下拉菜单中引起选择以调用功能。 如果要使用-em> mybutton.action = #selector(some_function(_:)),我可以做到这一点,但是它与NSCombobox无法使用。 这是我的代码中的一个示例:
func populate_scroller_with_combobox(json_file: Array<Any>, panel: NSView)
{
let combox = NSComboBox()
combox.identifier = NSUserInterfaceItemIdentifier(rawValue: "combobox1")
combox.addItem(withObjectValue: "None")
combox.addItems(withObjectValues: json_file_content)
combox.numberOfVisibleItems = 10
combox.isEditable = false
combox.action = #selector(some_function(_:))
combox.selectItem(withObjectValue: "None")
panel.addSubview(combox)
combox.frame = CGRect(x:190, y: 30, width: 170, height: 26)
}
@objc func some_function(_ sender: NSButton)
{
print ( "Combobox value changed." )
}
I'm creating a combo box programmatically and I would like to cause the selection from the drop down menu to call a function.
I can do it if i'm creating a button using - myButton.action = #selector(some_function(_:)), but it doesn't work with an NSComboBox.
here is an example from my code:
func populate_scroller_with_combobox(json_file: Array<Any>, panel: NSView)
{
let combox = NSComboBox()
combox.identifier = NSUserInterfaceItemIdentifier(rawValue: "combobox1")
combox.addItem(withObjectValue: "None")
combox.addItems(withObjectValues: json_file_content)
combox.numberOfVisibleItems = 10
combox.isEditable = false
combox.action = #selector(some_function(_:))
combox.selectItem(withObjectValue: "None")
panel.addSubview(combox)
combox.frame = CGRect(x:190, y: 30, width: 170, height: 26)
}
@objc func some_function(_ sender: NSButton)
{
print ( "Combobox value changed." )
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果其他人遇到同一问题,这是解决方案:
添加到您的ViewController类“ NSComboboxDelegate”中。
在您的ComboBox创建功能中添加'combox.delegate = self'。
添加另一个称为func的函数“ ComboboxSelectionDidchange(_通知:通知)”,该功能将由选择更改触发
here is the solution in case that anyone else run into the same issue:
add to your ViewController Class 'NSComboBoxDelegate'.
to your combobox creation function add 'combox.delegate = self'.
add another function called func 'comboBoxSelectionDidChange(_ notification: Notification)' which will be triggered by the selection changes
自从我使用NSCombobox以来,这是一个Looooonnnnggg的时间。
回顾文档,它似乎遵循数据源&amp;表视图使用的委托模式。
您需要让一些对象符合NSComboboxDelegate协议,并将其设置为组合框的代表。然后,当用户进行选择时,您的代表的
comboboxSelectionDidchange(_:)
方法将被调用。您可以创建一个自定义
nscontrol
,该管理nscombobox
并用作组合框的代表,并且当用户选择一个valuechange的事件类型时,控件会生成IBACTION物品。It's been a looooonnnnggg time since I've used an NSComboBox.
Reviewing the docs, it seems to follow the data source & delegate pattern that table views use.
You need to have some object conform to the NSComboBoxDelegate protocol and set it as the combo box's delegate. Then your delegate's
comboBoxSelectionDidChange(_:)
method will be called when the user makes a selection.You could create a custom
NSControl
that manages anNSComboBox
and serves as the combo box's delegate, and have the control generate an IBAction with an event type of valueChanged when the user selects an item.