问题
I have a macOS project where there are two tableView with the same viewControl as delegate. How can I learn which of the two is called on tableViewSelectionDidChange:?
EDIT: I'm using tableViewSelectionDidChange:
to prevent some items I use as a sort of "title group" from being clicked.
I read this question NSTableViewDelegate with 2 tables and tableViewSelectionDidChange:(NSNotification *)aNotification but being a beginner, I don't know how to do this in Swift.
I tried
func tableViewSelectionDidChange(_ notification: Notification) {
let tableViewName = (notification.object? as AnyObject).identifier // error
if tableViewName == myTableView1 {
print("myTableView1")
}
}
but I get the "Ambiguous use of Identifier" error. Is somebody so kind to tell me what I'm doing wrong? A working example would be very appreciated.
回答1:
From the documentation of NSTableViewSelectionDidChangeNotification
The notification object is the table view whose selection changed. This notification does not contain a userInfo dictionary.
So the object is clearly non-optional and a table view instance. It's not AnyObject
let tableView = notification.object as! NSTableView
if let identifier = tableView.identifier, identifier == "myTableView1" {
print("myTableView1")
}
来源:https://stackoverflow.com/questions/45117283/how-to-learn-in-swift-inside-tableviewselectiondidchange-in-which-nstableview