@interface MyTableViewDelegate : NSObject <NSTableViewDelegate, NSTableViewDataSource>
{
// This iVar needs to be connected to the table view via the IB.
IBOutlet NSTableView *tableOutlet;
}
@property (assign) IBOutlet NSTableView *tableOutlet;
- (void)doubleClick:(id)nid;
@end
//执行摘录。
@implementation MyTableViewDelegate
@synthesize tableOutlet = _tableOutlet;
- (void)awakeFromNib {
[_tableOutlet setTarget:self];
[_tableOutlet setDoubleAction:@selector(doubleClick:)];
}
- (void)doubleClick:(id)object {
// This gets called after following steps 1-3.
NSInteger rowNumber = [_tableOutlet clickedRow];
// Do something...
}
//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
}
然后我们需要一个函数来执行表格视图的双击,这个函数是用 # 选择器调用的,因此需要对 Objective C 可见(即使我们在 Swift 中编程) ,我们只是简单地用@Objective 启动这个函数。
@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)
}
}