7

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

I'd like to fire an action when the user clicks on the text field (label) inside the table's view (imagine having a hyperlink with a custom action in each table cell).

I've created a basic NSTextField subclass to catch mouse events. However, they only fire on the second click, not the first click.

I tried using an NSButton and that fires right away.

Here's 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 Answers 5

10

Had the same problem. The accepted answer here didn't work for me. After much struggle, it magically worked when I selected "None" as against the default "Regular" with the other option being "Source List" for the "Highlight" option of the table view in IB!

Edit: The accepted answer turns out to be misleading as the method is to be overloaded for the table view and not for the text field as the answer suggests. It is given more clearly at https://stackoverflow.com/a/13579469/804616 but in any case, being more specific feels a bit hacky.

2
  • 2
    it seems that this works actually. I was trying to trigger mouseUp, but that doesn't work with either methods. MouseDown works with both Feb 26, 2015 at 22:10
  • While this does work, what if you want the table view to use the source list table view style? Jan 18, 2018 at 10:54
7

It turned out that NSTableView and NSOultineView handle the first responder status for NSTextField instances differently than for an NSButton.

The key to get the label to respond to the first click like a button is to overwrite [NSResponder validateProposedFirstResponder:forEvent:] to return YES in case of my custom text field class.

Documentation:

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/NSResponder/validateProposedFirstResponder:forEvent:

1

The behavior that you're seeing is because the table view is the first responder, which it should be or the row won't change when you click on the label -- this is the behavior that a user expects when clicking on a table row. Instead of subclassing the label, I think it would be better to subclass the table view and override mouseDown: there. After calling the super's implementation of mouseDown:, you can do a hit test to check that the user clicked over the label.

@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
4
  • Thanks, I'll try that. One question: the first click does work with an NSButton (the table row is not selected in that case). Is there any way to emulate that behavior?
    – Mark
    Mar 24, 2012 at 8:52
  • There probably is, but I don't know it. I would have thought that returning YES for acceptsFirstMouse and/or acceptsFirstResponder would have done that.
    – rdelmar
    Mar 24, 2012 at 15:49
  • If you want the behavior you get with a button, why not just use a button? You can turn off the border, and it would look like a borderless label anyway. If you want a border, you could embed a borderless button in an NSBox.
    – rdelmar
    Mar 24, 2012 at 16:06
  • Because using a label makes it easier to align it with other labels inside the view. Also, using a button has other unwanted side effects such as showing a different background color when pressing the button.
    – Mark
    Mar 25, 2012 at 5:08
1

In the exact same situation, embedding an NSButton with transparent set to true/YES worked for me.

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 begin editing the text field. You send this message from the override mouseDown(withEvent TheEvent:NSEvent) method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.