0

I have an NSSplitView showing two NSTableView instances. I need to detect which table view has become "active" (of focused), which means the one that the user has clicked. I need to know that because each table view acts as a source list for another view that shows the content of the selected row(s). This other view is shared for both tables.

I could do it by subclassing NSTableView and reacting to mouseDown: or another method but it I'd rather avoid subclassing just for that. I also don't want to track any NSWindow event just to know if the user has clicked one of the tables (I'd rather subclass NSTableView).

Currently, I use the delegate method tableViewSelectionDidChange:, but this method is, obviously, only called when the selected row changes. I need to know that a table becomes active even if the selected row hasn't changed.

Observing the clickedRow property of the table views doesn't appear to work. If may not be KVO compliant.

Any ideas?

jeanlain
  • 382
  • 1
  • 3
  • 13
  • If the window is the active window you can check the first responder `window.firstResponder == tableView1` – vadian May 18 '22 at 09:03
  • Do you want to detect which table view is active or do you want to detect when a table view becomes focused? – Willeke May 18 '22 at 09:16
  • Hi @Willeke, I want to detect when a table view becomes active, so as to react to the change. I don't want to determine which is active at a random point in time. Which is why the suggestion by @vadian isn't really useful for me. Right now, I'm subclassing NSTableView to send a message to its delegate when it becomes first responder (I override `becomesFirstResponder`). I may stick to this solution if I can't find a simpler one. – jeanlain May 18 '22 at 09:54

1 Answers1

0

For those interested, the most convenient solution I found was to take advantage of the fact that NSTableView is a subclass of NSControl. So just like NSButton it can send action messages when clicked (upon mouse up). For each tableView, I wired its "action" to the same ibaction selector of my controller object in interface builder. The controller identifies the sender and acts accordingly. No need to subclass NSTableView.

jeanlain
  • 382
  • 1
  • 3
  • 13