Corbin's Treehouse - Corbin Dunn, Santa Cruz, CA
Plug Bug
Treehouse
Photography
Videos
Projects
Unicycling
About

NSTableView Tips: Not delaying the first responder


I have a little home-brew Cocoa app for making Cyr Wheel patterns. The UI is built with an NSTableView and looks like this:

Screen Shot 2014-04-26 at 7.31.00 PM.png

Now normally when you try to directly click on one of the NSTextFields the table will first select the row. Then a second click will allow you to start editing. For most tables, this is the behavior you want. For something like what I have, I want to avoid this, and allow the first responder to go through. This can easily be done by subclassing NSTableView and overriding:

- (BOOL)validateProposedFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event {
   return YES; // no delay
}

NSTableView’s default logic is a bit complex that allows things to go through or not based on several properties. This method is declared in NSResponder.h, and it is sent up the responder chain when the text field itself attempts to become first responder. When NSTableView gets it, it does some determination to determine if it should go through or not. If it wasn’t an already selected row, it returns NO; this causes the table to get the mouseDown: and do the selection logic. br/>I don’t care what control was clicked; I want it to go through without selecting, and always return YES.



2 Responses to “NSTableView Tips: Not delaying the first responder”

  1. aure says:

    woohoo!
    Most overlooked method ever. Thanks a lot, that’s *really* helpful. I made horrible hacks to achieve the (somewhat) same result in the past!

  2. aure says:

    more more more more Cocoa posts please :D
    WWDC demo ?

(c) 2008-2012 Corbin Dunn

Corbin's Treehouse is powered by WordPress. Made on a Mac.

Subscribe to RSS feeds for entries and comments.

17 queries. 0.471 seconds.