I need my application to open a window when a user double clicks on a row in an NSTableView
. I\'m having a bit of a difficult time finding information or exampl
On SWIFT 4.1 You set the doubleAction method of the TableView object inside your code to perform an @objc function by using a #selector(nameOfYourFunction)
Inside this function you call a segue. You can link your new window to the origin window on InterfaceBuilder (not to the NSTableView object but the actual ViewController object.
Then do all your setup for the new window on prepare for segue:
Alright first on Interface Builder:
Of course give an identifier to that segue:
Next, inside our first view controller (where the table view is) code:
//We use this function: prepare for segue
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
// check if we are referring to the actual segue we want
if segue.identifier?.rawValue == "segueToYourNewWindow" {
// now create a reference to that new window
let yourNewWindow = segue.destinationController as! newWindowViewController
// now change variables inside that view controller code, remember that the objects might fail if they are not yet visible to the user so first set up the variables or call them using the main thread, up to your design.
yourNewWindow.selectedRowVariable = thisTableView.clickedRow
}
Then we need a function to perform the segue on the table view's double click, this function is called with a #selector and therefore needs to be visible to Objective C (even that we are programing in Swift) we just simply start the function with @Objc thats it.
@objc func doubleClickOnResultRow() {
//beware of double-clicking also triggers this function when no rows is selected with the selectedRow being -1
if (thisTableView.selectedRow > -1 ) {
performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "segueToYourNewWindow"), sender: nil)
}
}
Finally we set this function to the doubleAction method of the TableView in the initial setup part of our code like this:
override func viewDidLoad() {
super.viewDidLoad()
thisTableView.doubleAction = #selector(doubleClickOnResultRow)
}