UITextField - Action on resignFirstResponder -- possible? - variables

I have created a form that has a number of text fields that save to NSStrings that are then used deeper in the app.
Currently, if you hit the return key (done) on the GUI keyboard, the keyboard is dismissed, and the data in the text field is saved to its NSString. Likewise, if the background is touched the same occurs.
However, if I touch another text field, without doing one of the above first, the variable is not saved. Is there a way to do this? Perhaps a way to perform an action when a specific text field release firstresponder?

The best solution for your issue is just to use the
-(void)textFieldDidEndEditing:(UITextField *)textField
And then you can just access the textField and store the textFields text in the appropriate string every time focus is lost.

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.

How to handle first responder status for NSTextField subclass?

I'm working on a project which needs a special text field to edit byte values. My current solution is a dedicated readonly textfield and a "..." button to open a popover as shown in the image below:
Now I try to make my solution more user friendly. My goals are these:
If the text field gets the first responder status, the popover automatically opens.
The complete text is selected.
If the user leaves the text field with tab or selecting any field outside of the popover, the popover should automatically close.
If the user types any valid number and suffix the byte value is updated (e.g. "10 GB")
Currently I'm a little bit clueless. My questions are these:
Where is the best location to detect in in the subclass when the text field got first responder?
How can I detect when the field resigns being first responder?
Are there other, simpler solutions?
I could implement everything using - (BOOL)becomeFirstResponder as a hook to display the popover and observing the first responder to automatically hide the popover:
- (void)viewDidMoveToWindow
{
[super viewDidMoveToWindow];
[self.window addObserver:self forKeyPath:NSStringFromSelector(#selector(firstResponder)) options:0 context:NULL];
}
As a start point, I published a working project with the classes on GitHub (MIT License):
Project on GitHub

NSTextField with built in validation

I have in my app an NSTextField for creating a new file. I would like to have this textfield smart enough to show a small icon which show if the currently entered filename is valid (not yet exists) or not.
This icon should also change when the textfield contents change.
What is the way to achieve that?
Thanks!
This is the method you are looking for:
- (void)controlTextDidChange:(NSNotification *)aNotification
This is called by the textfield's delegate when the text changes. Perform your validation in the method and update your UI accordingly.

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].

Manipulating textfield values in an iPhone app

How do I manipulate textfield variables within an iPhone app? I want to have three text fields, formatted to numbers only, and be able to manipulate the data when I press different buttons.
Any help would be appreciated!
UITextField objects have a "text" property of type NSString that can be read and written. Keep a reference to the text fields, and when a button is pressed, update the field's text property with a new NSString to change its contents. If the text fields are not directly editable by the user, you may want to use UILabel instead, which is basically just a non-editable text field (and which also has a read/write "text" property).
You may also want to read about the NSNumberFormatter class that allows you to format the input into whatever way is relevant to your application (currency values, floats, etc).
To solve the problem of editing certain text fields, you could disable the user interaction until a button is pressed.