NSTextField and hidden property - objective-c

I have an NSTextField that I hide when the user presses a button. I hide the text field using [textField setHidden:YES]; The problem is that is the user is typing in the text field (i.e. the text field is first responder) and the user presses the return key (which is the key equivalent for the button that hides the text field) the user can keep typing in the text field even though its not visible. How do I correctly remove a text field without actually deallocating it?
Thanks.

Try changing the first responder after hiding it.
[[textField window] makeFirstResponder:[textField nextResponder]];

Related

UITextField losing focus after button press and text field switch (keyboard remains visible)

I've got a UITableViewCell subclass here that manages a UITextField setup on the right side of the table view cell. Anywhere from 4 to 8 of these cells are displayed at any given time depending on the table; I use them for unit data entry (ie, entering in distances, temperatures, etc) so there's quite a bit of logic bolted to the cell subclass.
For whatever reason, I've noticed that if I perform the following steps:
1) Tap on a text field to begin editing and bring up the keyboard
2) Enter in some text
3) Tap the clear button (which is enabled on the text field)
4) Tap on another textfield in the same table view
Then the current UITextField loses focus, but the second text field does not gain it. This means that no UITextField currently has focus, but the keyboard is still being displayed on-screen... but without any active text field, it does nothing, and cannot be dismissed (presumably because there's no first responder to resign?).
I can then tap on another text field again, and it will take focus and begin editing- at which point the keyboard becomes operable again and pressing the return/done key will dismiss it and end editing as usual.
If I simply tap on another UITextField without first hitting a button, then the second UITextField will gain focus immediately (as I'd expect it to). But it seems like button presses outside of the UITextField will cause this behaviour to occur if you try to switch fields after tapping any kind of button other than the text field.
Does anyone know what is causing this? It almost sounds like there's something wrong with the responder chain, but I'm not sure what the problem would be or how to fix it.
Firstly,you are sure the textfiled in the table has a unique identifier ,such as tag.
Secondly,you should make another textfiled become first responder if you want a textfiled lost first responder but the keyboard still appear.
Figured out what it was...
The problem was that I was reloading the table data in the delegate method that my custom cell was calling upon edit completion. Apparently reloading the tableview data while you're in the middle of switching UITextFields will cause the second text field to not gain focus (but the keyboard won't get dismissed), hence causing the issue I was seeing.

NSDatePicker becomeFirstResponder

I have a data entry form where the first field the user wants to enter is an NSDatePicker.
I wish this field to be highlighted when the user clicks a button (labelled 'New')
I have tried putting in [myDatePicker becomeFirstResponder] but this has no effect.
Can anybody show me how to set the focus to myDatePicker?
Unfortunately, becomeFirstResponder does not work that way.
As mentioned in the docs, you should send makeFirstResponder: to the window that contains the control you wish to become the first responder. In your case the window that holds your NSDatePicker.
becomeFirstResponder can be overridden in a control to return false if you do not want it to become the first responder.
Is your picker linked to a textfield for the output? I have linked custom pickers to my textfields keyboard property before. If the output is set to the textfield, you can make the textfield the first responder and the picker will come up in place of the keyboard.

NSTextField simulate return pushing a button

I have an NSTextField with value bound to a Shared User Default Controller
The NSUserDefault value change only when user press "Enter" ok keyboard.
I'd like to produce the same effect (simulate the return hit on a specific field) when user push a button on the UI.
Is it possible ?
In your bindings, you can check the "Continuously Updates Value" box to have the user defaults update as you type, so if your button method just checks the value, it will always be up to date. A more general solution to update the value of a text field is to have the window make something other than the text field the first responder (this is what happens when you tab out of a text field to the next responder). You can do this with:
[window makeFirstResponder:window];
In your button's action, do this:
[[NSUserDefaultsController sharedUserDefaultsController] commitEditing]

Adding an NSTextField as a subview

I'm trying to add an NSTextField as a subview of a custom view class I have (which subclasses NSView), and then make the NSTextField the first responder. This works fine. The text field shows up and I can start typing in it. However, any mouse events in the text field seem to fall through to its superview. For example, I can't see the mouse cursor when I hover over the text field, and when I click anywhere in the text field, it attempts to resign firstResponder status instead of letting me select text within the text field.
I'm not overriding hitTest or anything weird like that, and I only have one window, which is definitely the key window. Any ideas?
Thanks in advance! :-)

NSTextField setEnabled

I have a NSTextField object in my window which has to be disabled when a check box is clicked.
I have written a IBAction to receive the check box click and disabled/enabled the text filed based on the check box state.
[mName setEnabled: [mNameCheck state]];
This work fine with the basic functionality, but I found some strange behavior.
You update some detail in the text filed and click on check box the text filed get disabled old content.
Example:
Stage 1: Text filed has the content
"Name"
Stage 2: Update the text filed
content as "Girish"
Stage 3: Click check box (to disable
the text filed)
Stage 4: Text filed disable with the
content as "Name"
The issue get resolved if I resign the responder and set responder to some other controller before the text field is disabled.
In my case I can not assign the responder to check box(it does not take) or any other controller so I did some thing like bellow which works fine
[mName resignFirstResponder];
[mName becomeFirstResponder];
resign and assign responder with same controller.
I am just wondering is this solution is correct or any better solution to this issue?
As the docs state, do NOT call -resignFirstResponder or -becomeFirstResponder directly. Call -[NSWindow makeFirstResponder:] instead. It is acceptable to pass in nil and status will pass to the window itself.
You could try calling -[NSWindow selectNextKeyView:] although I'm not entirely certain what will happen if it doesn't find a valid next key view. Try it and see. If that doesn't work you'll have to fallback to calling -nextValidKeyView and -makeFirstResponder yourself.
If you set the text field to be Continuous in Interface Builder, the value of the field will be set as soon as a change is made in the field rather than when it loses focus. You can programmatically set this value with [yourTextField setContinuous:YES].