NSTableView Tips: Not delaying the first responder

Cocoa, Coding

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.



Subscribe
Notify of
guest

2 Comments
Inline Feedbacks
View all comments
aure

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!

aure

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

Subscribe to new posts:

You'll get an email whenever a I publish a new post to my blog and nothing more. -- Corbin

As an Amazon Associate I earn from qualifying purchases.

(c) 2008-2024 Corbin Dunn

Privacy Policy

Subscribe to RSS feeds for entries.

76 queries. 0.184 seconds.

Log in