Question

I have a custom NSViewController with two NSTableViews side-by-side, something like a split-view setup where the selection on the left tableView changes the right tableView's list. I'm not sure how to handle NSMenuItem events in this case. For e.g. if I press the Delete button, how do I distinguish between whether it's the left tableview or the right tableview that was highlighted when the Delete button was pressed? All I get is the delete: selector called with the NSMenuItem as the sender.

Was it helpful?

Solution

First, some background:

In Cocoa terminology, the "active" view or control is known as the "first responder." For instance, when you're entering text into a text field, the text field is considered the "first responder" because it's the object that's first to respond to keyboard input. An NSTableView can also receive first responder status (you can control the selected row by using the arrow keys).

You can ask the window for it's first responder like so:

// it's not necessarily a sure thing that the first responder is a TableView.
id myFirstResponder = [_parentWindow firstResponder];

if (myFirstResponder == _leftTableView) {
    // left tableview is selected
} else if (myFirstResponder == _rightTableView) {
    // right tableview is selected
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top