I got 2 textField and when the user types something in, and pushes the return button or back button on my navigation controller, that it saves the textFields. I've already tried this:
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
NSLog(#"textFieldShouldEndEditing");
return YES;
}
but this doesn't print out anything. The names of my two textFields are self.CellOne.textField and self.CellTwo.textField and I've also added the <UITextFieldDelegate> delegate in my class. What I actually want is whenever the foces goes off the textFields, it needs to be saved.
Related
I have two instances of a UITextField. The returnKeyType of the first text field is UIReturnKeyNext, and the returnKeyType second text field is UIReturnKeyDone. Based of this SO answer, I'm trying resign the first responder of the first text field when the 'Next' button is clicked, and then have the second text-field become the first responder. When the 'Done' button is clicked on the second text-field, the first responder is resigned, and the keyboard disappears.
Below is my code for this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == _textFieldOne){
[_textFieldOne resignFirstResponder];
[settingsDictionary setObject: _textFieldOne.text forKey:#"TextFieldOneInfo"];
[settingsDictionary stringValueForKey:#"TextFieldOneInfo"];
[self postNotificationSettingsUpdate:settingsDictionary];
didTestPostNotificationSettings = YES;
[_textFieldTwo becomeFirstResponder];
}
if (textField == _textFieldTwo){
[_textFieldTwo resignFirstResponder];
}
return YES;
}
When the 'Next' button is clicked, the first text-field successfully resigns the first responder, and the second text-field does become the new first responder. However, the second text-field then immediately seems to resign it's first responder status, before the 'Done' button is clicked.
Can anyone tell me why the second text-field is resigning it's first responder status before it's 'Done' button is clicked? Thank you!
EDIT I've narrowed down the problem to the following line of code:
[self postNotificationSettingsUpdate:settingsDictionary];
When it's commented out, text field return-button actions behave as expected.
You don't need to call resignFirstResponder if you want switch textFields. I have successfully tried this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == _textFieldOne){
[_textFieldTwo becomeFirstResponder];
}
else if (textField == _textFieldTwo){
[textField resignFirstResponder];
}
return YES;
}
After some digging, I believe I found a solution.
The specific offending line of code is below:
[self postNotificationSettingsUpdate:settingsDictionary];
which calls the method below:
- (void)postNotificationSettingsUpdate:(NSDictionary *) updateDict {
[self.dataCache setUserNotificationSettings:updateDict];
}
...which sends the the dictionary information over a network connection.
These text views are stored in a UITableView row. What appeared to me to be happening was that when this method was being called, it reloaded the view. The solution was to add calls to [tableView beginUpdates]; and [tableView endUpdates]; at the beginning and end of
- (BOOL)textFieldShouldReturn:(UITextField *)textField.
After doing that, the second-text field would become the first responder when the 'Next' button of the first-text field was selected, and would resign it's first responder status when the 'Done' button was selected.
I'd like to achieve the following behaviour of a single UITextField and the keyboard:
when the view has loaded UITextField becomes first responder and the keyboard opens (so far so good):
- (void)viewDidLoad
{
[self.editField becomeFirstResponder];
self.editField.delegate = self;
}
Now user inputs some text and presses the return key. That text is added to the data source in the following method:
- (IBAction)didEndOnExit:(id)sender {
//add self.editField.text to data source
}
Now after the return key is pressed and the above UITextField's method gets called and executed I would like the UITextField to clear, the cursor to be placed and be visible at the beginning of the text field and the keyboard not to hide so that new item could be entered in the textfield and added to the data source.
This is how I return to the previous view in the app (using a button):
- (IBAction)backButtonPressed:(id)sender {
[self dismissViewControllerAnimated:NO completion:^{}];
}
I tried playing with UITextFieldDelegate's methods and UITextField's becomeFirstResponder and resign FirstResponder, but am unable to achieve the above described behaviour. I've seen posts here on Stack Overflow about using consecutive UITextField's to enter data, but not to use the same UITextField time and again.
You should override the below mentioned method and do whatever you want and showing the keyboard too.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Have a try :)
While dismissing view controller all other code executions freezes when (view controller is being dismissed).
Use some delay (performselectorafterdelay). or
completionhandler of dismissingviewcontroller for executing your code when viewcontroller is being dismissing and
Also execute code on main Thread which includes UI changes or updates
after saved your data.. you have to make the uitextfield to be nil... like self.editField.text = nil. then you can call [self.editfield becomesfirstresponder].. after you will add more items to array or else anything everytime the field should be nil after you added..
From the Doc
Return Value: YES if the text field should implement its default
behavior for the return button; otherwise, NO.
For EX - Use the Delegate of UITextField :
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//add self.editField.text to data source
textField.text = #"";
return NO; // This will do your need
}
Because I used UITextField's action method - (IBAction)didEndOnExit:(id)sender the keyboard always got closed. Apparently that method makes the UITextField resign from being first responder. Instead of adding data to data source in that method, it can be added in the delegates's method - (BOOL)textFieldShouldReturn:(UITextField *)textField and so the keyboard won't close.
I have a few UITextFields (within UITableViewCells) on my UIView with a "Save" UIButton. I want to do some basic validation on the UITextFields when the user clicks the "Save" button.
I have overridden textFieldDidEndEditing to save each of my UITextField data to an instance variable; however, if a user clicks the save button before either clicking the "Return" button of the UIKeyboard or clicking on another UITextField the data in my last UITextField is never saved to my instance variable and validation always fails.
I am looking for a way to trigger an "onBlur" (I know that's a JS thing)-type event to save my string in UITextField to my instance variable.
I've looked through the UITextFieldDelegate Protocol and I do not see anything like this.
Is there a method I may be missing?
to trigger textFieldDidEndEditing on your UITextField, you will need to call
[_txt resignFirstResponder];
were _txt is your UITextField
Please note that if you dont have a reference to _txt and you need to find the first responder in order to resign it
You could use the solution from this question Get the current first responder without using a private API
Then instead of calling
[_txt resignFirstResponder];
you would call
[self.view findAndResignFirstResponder];
Try this
// if we encounter a newline character return
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// enter closes the keyboard
if ([string isEqualToString:#"\n"])
{
[textField resignFirstResponder];
return NO;
}
return YES;
}
which will trigger
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
// Call webservice
return YES;
}
my objective simply to save text on UITextField after user click done button on keyboard. I could either do this in extFieldShouldReturn or textFieldDidEndEditing: does it make any difference ? or there a better approach ?
Thanks!!
textFieldShouldReturn is only called if the user presses the return key. If the keyboard is dismissed due to some other reason such as the user selecting another field, or switching views to another screen, it won't be but textFieldDidEndEditing will be.
The best approach is to use textFieldShouldReturn to resign the responder (hide the keyboard) like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//hide the keyboard
[textField resignFirstResponder];
//return NO or YES, it doesn't matter
return YES;
}
When the keyboard closes, textFieldDidEndEditing will be called. You can then use textFieldDidEndEditing to do something with the text:
- (BOOL)textFieldDidEndEditing:(UITextField *)textField
{
//do something with the text
}
But if you actually want to perform the action only when the user explicitly presses the "go" or "send" or "search" (or whatever) button on the keyboard, then you should put that handler in the textFieldShouldReturn method instead, like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//hide the keyboard
[textField resignFirstResponder];
//submit my form
[self submitFormActionOrWhatever];
//return NO or YES, it doesn't matter
return YES;
}
How do you programmatically set focus to some UITextField in iOS?
When you tap on a textfield, the keyboard pops up to begin editing the textfield.
How can you give focus to a textfield and bring up the keyboard without requiring the user to tap on the textfield?
Make the textField the first responder; this will also popup the keyboard:
[self.textField becomeFirstResponder];
And just some more typical example code which may help someone,
in your storyboard, click on one of the text fields, and set the outlets-delegate to be the class in question. In that class, in the .h file make sure that you declare that it is a UITextFieldDelegate
#interface Login : UIViewController <UITextFieldDelegate>
Then in the .m file, you could have this for example...
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if ( textField == self.loginEmail ) { [self.loginPassword becomeFirstResponder]; }
if ( textField == self.loginPassword ) { [self yourLoginRoutine]; return YES; }
return YES;
}
When a user clicks the "Next" button on the virtual keyboard - on the 'email' field - it will correctly move you to the password field. When a user clicks the "Next" button on the virtual keyboard - on the 'password' field - it will correctly call your yourLoginRoutine.
(On the storyboard, on the attributes inspector, notice that you can select the text you want on the Return key ... here "Next" would work nicely on the email field and "Done" would work nicely on the password field.)
For changing focus on the fly:
[yourTextField becomeFirstResponder];
For Swift, you can use:
theTextFieldYouWant.becomeFirstResponder()