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

浪子不回头ぞ 提交于 2019-12-13 17:20:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!