这就是我最终做的事情。我做了一个 NSTextView 的子类并覆盖了 mouseDown: 如下...
- (void)mouseDown:(NSEvent *)theEvent
{
// Notify delegate that this text view was clicked and then
// handled the click natively as well.
[[self myTextViewDelegate] didClickMyTextView:self];
[super mouseDown:theEvent];
}
我正在重用 NSTextView 的标准委托...
- (id<MyTextViewDelegate>)myTextViewDelegate
{
// See the following for info on formal protocols:
// stackoverflow.com/questions/4635845/how-to-add-a-method-to-an-existing-protocol-in-cocoa
if ([self.delegate conformsToProtocol:@protocol(MyTextViewDelegate)]) {
return (id<MyTextViewDelegate>)self.delegate;
}
return nil;
}
在标题中...
@protocol MyTextViewDelegate <NSTextViewDelegate>
- (void)didClickMyTextView:(id)sender;
@end
在委托中,我实现了 didClickMyTextView: 来选择行。
- (void)didClickMyTextView:(id)sender
{
// User clicked a text view. Select its underlying row.
[self.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[self.tableView rowForView:sender]] byExtendingSelection:NO];
}