Get the representedObject values of NSCollectionViewItem NSButton click

前端 未结 1 1981
傲寒
傲寒 2020-12-10 23:11

I have read some questions and I find some very confusing and I don\'t really know if they answer my question.

I have an NSCollectionView implemented an

相关标签:
1条回答
  • 2020-12-10 23:40

    An action method takes one argument:

    - (IBAction) collectionViewButtonClicked:(id)sender {
    }
    

    That sender is the control or other UI element (e.g., menu item) that sent the message.

    With that argument, when your action method gets called, you know which button was clicked.

    A button is a kind of control, and every control is backed by at least one cell. Cells have represented objects, too.

    So, first, set the represented object of your button's cell to the collection view item that owns the button. (You can do this in the nib editor.) Then, in your action method, get the button's cell, then the cell's represented object (which is the item), then the item's represented object.

    If the representedObject outlet doesn't show up in the nib editor, you probably have the button selected, not its cell. I recommend opening the nib editor's outline view using the button in the lower-left and then never, ever closing it.

    0 讨论(0)
提交回复
热议问题