NSCollectionView 在按钮单击时获取所选项目
我有一个 NSCollectionView 绑定到一个 NSArrayController ,该控制器以 NSMutableArray 作为内容。 NSMutableArray
中的项目按其应有的方式显示。
我有一个 WindowController 类,它包含对 NSMutableArray 的引用。还有一个按钮会导致 buttonClicked
消息发送到我的 WindowController。
现在在 buttonClicked
中,我想知道在 NSCollectionView
中选择了哪个项目。我该怎么做?我没有对 NSArrayController 的引用,否则我可以通过发送选择来询问它。我研究了一些示例代码但无济于事。
如何获取对 NSArrayController 中当前所选项目的引用?
I have a NSCollectionView
is bound to an NSArrayController
that has an NSMutableArray
as content. Items in the NSMutableArray
are displayed as they should.
I have an WindowController class that holds a reference to the NSMutableArray
. There is also a button that causes a buttonClicked
message to be sent to my WindowController.
Now inside buttonClicked
I want to know what item is selected in the NSCollectionView
. How do I do this? I have no reference to the NSArrayController
otherwise I could have asked it by sending selection
. I've studied some sample code but to no avail.
How can I get a reference to the currently selected item in the NSArrayController
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 NSCollectionView 正在从 NSArrayController 获取其内容,因为 IB 中在
content
上设置了绑定,或者您已以编程方式绑定了这些内容,那么也是 collectionView 的selectionIndexes
上可用的绑定,当绑定到 arrayController 时,它将为您提供 arrayController 和 collectionView 中“对当前所选项目的引用”。NSCollectionView 的 itemAtIndex 提供当前所选项目的索引,将为您提供所选的实际项目。
实际选择的项目是您想要获取参考的内容吗?
if the
NSCollectionView
is getting its content from theNSArrayController
, as there is a binding set in IB oncontent
or you have programmatically bound these, there is also a binding available on the collectionView'sselectionIndexes
, which when bound to the arrayController will give you 'a reference to the currently selected item' in the arrayController and the collectionView.NSCollectionView's itemAtIndex, handed the currently selected item's index, will get you the actual item that is selected.
is the actual item that is selected what you are looking to obtain a reference to?