codingFriend1
codingFriend1

Reputation: 6807

Get row of NSPopUpButton in view based NSTableView

I have a view based table view with one row containing NSPopupButtons. When the user changes the popUpButton I need to get get the table row in which this popUpButton is contained.

I first expected to get the row with

NSInteger clickedRow = [tableView rowForView: ((NSPopUpButton*) sender)];

But the sender of the action always is a NSMenuItem object not the NSPopUpButton. NSMenuItem however is not a view so I cannot use rowForView with that.

Currently my IBAction looks like this:

- (IBAction)changedPopUp:(id)sender {
    NSMenuItem* selectedMenuItem = ((NSMenuItem*) sender);
    NSPopUpButton* popupButton = (NSPopUpButton*)[selectedMenuItem view];
    NSInteger clickedRow = [tableView rowForView:popupButton];
    //[...]
}

But the view property is not set automatically and I find it quite inconvenient to set it manually for every NSMenuItem.

Is there no easy way to get the table row?

Upvotes: 2

Views: 476

Answers (1)

Aderstedt
Aderstedt

Reputation: 6518

Don't set an action and target for each NSMenuItem. Set the action and target for the NSPopupButton instead. Then, sender will be the NSPopupButton instance and you can use rowForView.

The drawback is of course that you cannot have different actions for different menu items.

Upvotes: 1

Related Questions