Clean way to resign first responder of NSSearchField when done? - cocoa

In my application I have an NSSearchField that is bound to an arraycontroller which performs searches as the user types.
The problem is that the search field never resigns firstresponder-status once it receives it.
What would be the cleanest way of resigning firstresponder status when the user presses Enter or clicks outside of the search field? If possible I would prefer to do as much work as possible in Interface Builder.

[searchField.window makeFirstResponder:nil]
If you just want to get rid of the focus ring, you can disable it in the NIB.
If you want to resign first responder when the user clicks empty space in the window, you have to use a custom NSView as the window's content view and override mouseDown: to call the above method.

Related

NSTableView allow selection change without becoming first responder

I've a window with a split view. Left is a NSTableView, to the right a custom view.
When my custom view is active in a 'command mode' I need it to remain first responder status so it can receive a cancelOperation: event when the escape key is pressed. But I do want the user to be able to change the selection in the table view.
Unfortunately, as long as my custom view refuses to resign first responder status the table view doesn't respond.
How can I make sure that the table view allows changing the selection without becoming first responder? Or how can I make sure the cancelOperation: event is delivered to my custom view, while it's not first responder?
the table view allows changing the selection without becoming first responder
Don't do this. It will confuse the user. The table view should become first responder.
Or how can I make sure the cancelOperation: event is delivered to my custom view, while it's not first responder
Put Cancel button in the window with key equivalent Escape. Or if you don't want a button, let an object in the responder chain (view controller, window controller) catch the escape key by implementing cancel: and tell the custom view to cancel.

Enabled NSTableView refuses to resign first responder and eats all the events sent to first responder

I have an NSTableView and it eats all events sent to the first responder. Before adding table view, everything was perfect, and all the messages sent to the first responder were sent correctly to my NSDocument subclass.
As soos as I added table view, it insists on always having focus. The focus indicator around it never disappears unless an external sheet view controller (or something similar) gains focus. When table view has focus, my messages sent to the first responder doesn't work with keyboard shortcuts. They work when I click them from the menu bar by pointer though.
If I set "refuses first responder" it doesn't change anything. If I set "enabled" to false, then my app returns to normal behavior (though I obviously can't interact with the table view).
How can I prevent table view's "always focused" behavior. I've tried In a view-based NSTableView, how make a control the first responder with a single click?'s answer but didn't change anything.
Since I've subclassed NSTableView, I've overridden becomeFirstResponder and returned NO without doing anything, and it worked great! I can interact with the table view freely, it just doesn't gain focus.

Remove Focus from UITextField

I'm trying to remove focus from a UITextField and even though I resign it from being first responder, I'm still not able to have the cursor not focus on the text field.
I don't have any other input on the view to move the focus to and I don't want to create a dummy one either. What is a good workaround for this?
As per the documentation.
To dismiss the keyboard, send the resignFirstResponder message to the text field that is currently the first responder. Doing so causes the text field object to end the current editing session (with the delegate object’s consent) and hide the keyboard.
If you call resignFirstResponder on your textfield then it will end the editing session and the cursor wont be focussing on that textfield.
So please verify one more time whether resignFirstResponder is getting called on that textfield which you want to remove the focus.
Please try to set your current class as delegate of your UITextField. I think you forget to set the delegate that's why it's not working as you are expecting.

NSTextFinder action on NSTextView

I'm trying to capture all the NSTextFinderClient calls on my custom NSTextView subclass.
The show action is called on my -(void)performTextFinderAction:(id)sender override, but for find next, find previous, etc. it's not called.
Any ideas?
Thanks!
Edit:
If you create a new project and drag an NSTextView from interface builder, command-g and command-shift-g (find next and find previous) don't work when the find bar is first responder.
Why is this?
I need a custom subclass of NSTextView to respond to the find bar for every event.
I searched in the Apple's TextEdit source code because with TextEdit, the standard search bar within the Text View works fine for command-G (and other shortcuts) even the search field is the first responder.
I found the solution.
Go to your nib for the main menu, and select the "Find" (and related) menu items. They should be bound to the default action called "performFindPanelAction:." Now unbind them and bind to "performTextFinderAction:" of the First Responder instead.
You may not find that action in the First Responder's action list. So you need to add it by yourself in the First Responder's attributes inspector pane.
This was meant by the document below saying
Before OS X v10.7, the default action for these menu items was performFindPanelAction:. Whenever possible which you should update your implementation to use this new action.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/#//apple_ref/occ/instm/NSResponder/performTextFinderAction:
The find bar communicates privately with the client's NSTextFinder instead of calling NSResponder's -performTextFinderAction:. This is necessary to allow find to work when something besides the client has key focus.
What are you trying to accomplish?

How to implement key equivalent for NSSearchField

I have a Cocoa application I am building which contains an NSSearchField control. I want to enable a keyboard shortcut / key equivalent so when the uses presses COMMAND-OPTION-F, the search field gets focus.
However, after much searching, it is not clear to me what the best way to implement this is. There is not an option to set this for the NSSearchField in Interface Builder.
Is the solution to subclass NSSearchField and listen for the keyDown event (and then see if the key equivalent is pressed?)
You can always add a menu item with a key equivalent of ⌘⌥F i.e. CommandOptionF. In the menu's action, just manually make the search field the first responder using
[window makeFirstResponder:searchField];

Resources