This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have three text fields and I want to be able to move the cursor to the next one. When the last one is reached, the keyboard dismisses. But nothing works for me... Here's an example of what I have. However, nothing moves, and nothing happens when I select the next button.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == field1TextField) {
[textField resignFirstResponder];
[field2TextField becomeFirstResponder];
}
else if (textField == field2TextField) {
[textField resignFirstResponder];
[field3TextField becomeFirstResponder];
}
else if (textField == field3TextField) {
[textField resignFirstResponder];
}
return YES;
}
The object that this method is bound to should be set as the text field's delegate. Check this by setting a breakpoint in the method to verify that it is called when you think it is.
If you are using nibs or storyboards, the field... instance variables should be outlets that are correctly hooked up. If they are created programmatically, you should ensure that they have objects assigned to them. Verify this by inspecting the values of these variables when you are inside the method.
You don't have to call resignFirstResponder on other controls before calling becomeFirstResponder on another.
Related
This question already has an answer here:
resignFirstResponder for all textfields [duplicate]
(1 answer)
Closed 8 years ago.
I know that I can resign a single text field to first responder when touching a button by implementing the following:
- (IBAction)signMeUpButton:(id)sender {
[self.textfield resignFirstResponder];
}
However, In my case I have multiple text fields, and I feel like typing them in one at a time, cant possibly be the best way / most modern way of doing so.
I reviewed multiple questions on the site, such as this one:
iOS SDK: Dismiss keyboard when a button gets clicked?
But none of them mention, having more than one text field and dismissing them with an IBAction ... What can I do in this situation?
You may try this:
- (IBAction)signMeUpButton:(id)sender
{
[self.view endEditing:YES];
}
If you have more than one text field, it's best to implement the following
[self.view endEditing:YES];
So, in your case it would be :
- (IBAction) signMeUpButton:(id)sender {
[self.view endEditing:YES];
}
That will do exactly what you are looking for!
try this
UITextField *currentTextField;
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTextField = textField;
}
-(IBAction)click:(id)sender
{
[currentTextField resignFirstResponder];
}
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.
Is there any way to check the size of a message that is executed in your code? And how to get to it?
I have 3 textFields and delegated them, and used protocol
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self.toTextField resignFirstResponder];
[self.subjectTextField resignFirstResponder];
[self.bodyTextField resignFirstResponder];
return YES;
}
I want to see the size (RAM) that is taken up, by me clicking the return key on one of the keyboards.
You need to use Instruments to check the memory usage.
Check out this question.
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.
I know this is a basic question but i´m a little confused, so i hope you can help me. I have a tableview with multiple dynamic tableview cells, and inside each tableviewcell i have multiple textfields. Each cell has a different tag and also the textfields and i want to access the uitextfields values as you can imagine. My problem is, i´m not using IBoutlet for the textfields (it would be a enormous amount of IBoutlets)...I´m using - (void)textFieldDidEndEditing:(UITextField *)textField...but i just can´t seem to make the correct connections in the IB, this is my code:
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == [self.view viewWithTag:102]) {
[textField resignFirstResponder];
}
After this, do i have to connect the respective UItexfield (and all textfields) to self? and then, do i have to use the editing did end event?...
Regards
I guess that the answer to this question is another question: What do you want to do with the text that the user enters?
I assume that you have some kind of data model that you want to store the data in.
If so, then when this function is called, you need to take the text that is already in the textField and save it to your data model immediately as it is entered.
For instance, you can access the text that was entered like this:
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == [self.view viewWithTag:102])
{
[textField resignFirstResponder];
yourDataModel.stringToSave = textField.text;
}
}