MouseDown: in a custom NSTextField inside an NSTableView

I have a view based NSTableView. Each view in the table has a custom text field.

I would like to trigger an action when the user clicks on a text field (label) inside the table view (imagine that you have a hyperlink with a custom action in each cell of the table).

I created a base subclass NSTextFieldto catch mouse events. However, they only trigger the second click, not the first click.

I tried to use NSButtonand it fires right away.

Here is the code for the custom label:

@implementation HyperlinkTextField

- (void)mouseDown:(NSEvent *)theEvent {
    NSLog(@"link mouse down");
}

- (void)mouseUp:(NSEvent *)theEvent {
    NSLog(@"link mouse up");
}

- (BOOL)acceptsFirstResponder {
    return YES;
}

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {
    return YES;
}
@end
+5
source share
5 answers

. . , "" "", - " " "" IB!

: , , , . fooobar.com/questions/199332/..., , .

+7

, , , , , - , . , , , mouseDown: . - mouseDown: , , .

@implementation CustomTable

- (void)mouseDown:(NSEvent *)theEvent
{
    [super mouseDown:theEvent];
    NSPoint point = [self convertPoint:theEvent.locationInWindow fromView:nil];
    NSView *theView = [self hitTest:point];
    if ([theView isKindOfClass:[NSTextField class]])
    {
         NSLog(@"%@",[(NSTextField *)theView stringValue]);
    }
}

@end
+1

a NSButton transparent, true/YES.

class LinkButton: NSTextField {

    var clickableButton:NSButton?

    override func viewDidMoveToSuperview() {            
        let button = NSButton()
        self.addSubview(button)

        //setting constraints to cover the whole textfield area (I'm making use of SnapKit here, you should add the constraints your way or use frames
        button.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(NSEdgeInsetsZero)
        }
        button.target = self
        button.action = Selector("pressed:")
        button.transparent = true
    }

    func pressed(sender:AnyObject) {
        print("pressed")
    }
+1

You use window.makeFirstResponser (myTextfield) to start editing the text field. You are sending this message from the override method mouseDown (withEvent TheEvent: NSEvent)

0
source

All Articles