简体   繁体   中英

How to learn in Swift inside 'tableViewSelectionDidChange:' in which NSTableView the selection did change?

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.

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")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM