CocoaDev

Edit AllPages

I have added a find text field to my toolbar. The problem is that when the user press return in this text field (to search the current document of the same window), the document view is not the first responder (the text field is) – so how to handle this gracefully?

My application is not document based so going through the window controllers document doesn’t seem like an option (and this document could anyway be visualised in several different windows).


This question went by on the cocoa-dev mailing list a couple of days ago, and the answer was, well, not particularly graceful. Basically you have to either respond to the text field’s -controlTextDidBeginEditing delegate method or the NSControlTextDidBeginEditing notification, and when it comes, extract the field editor from the notification’s userInfo dictionary. Then, register yourself to receive the field editor’s NSTextDidEndEditing notification, and then extract the NSTextMovement key from that notification’s userInfo dictionary and compare it to NSReturnTextMovement to see if the user hit return. – Bo


Maybe I don’t understand the problem, but if you want to handle return in an NSTextField why not set the handler with the setAction: method? – parens


This whole long-winded explanation above was just me being an idiot; if you want to capture the return key only, just check the “Enter only” radio button in the text field’s info panel in InterfaceBuilder, or call [theTextField sendsActionOnEndEditing:NO];. You see, I read “Only on Enter”, and thought “Why would someone want their action method called when they entered (i.e. began editing) the field editor?”, which in retrospect was pretty silly. – Bo