stop keyboard dismissing when switching uitextf - ios

i am using a uitableview with one section and five cells, and two different uitextfields on each tableCell.
my problem is: when a user tabs on first textfield on first table row, keyboard comes up, then the user taps on the second textfield. the keyboard DISMISS and SHOW.
so how can i keep the keyboard up instead of DISMISS and SHOW when the user switching focus on textfields?
thanks.
first edit:
sorry for the late response on this, the code is a bit complicated to show in here. i do not resignFirstResponder/becomeFirstResponder in any of the textField delegate methods. could you please throw in any ideas on top of your head. thanks for all your help.
second edit:
sorry for my bad mistake, i totally misunderstood my problem. i will relink the new post in here in a minute.
third edit:
this is my new question

This should work for you... Just a guess work from my side as you had not posted any code:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField == firstTextfield)
{
if([[firstTextfield stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0)
return NO;
[firstTextfield resignFirstResponder];
[secondTextField becomeFirstResponder];
return YES;
}
else if(textField == secondTextField)
{
//Anything u want here
return YES;
}
return NO;
}

Normally the keyboard doesn't dismiss on its own when you switch fields.
If you are using a textfield delegate and are responding to "editingDidEnd" by resigningFirstResponder, then you will see that behavior.
If that is the case, after you leave the field, it is calling one of the methods and resigning the keyboard, then when you touch the other field, it is calling the firstResponder.
So, look for some code where you are setting a textfield delegate to call one of the textfield selector methods. Find the method that is being called and see if it is resigingFirstResponder.
If it is, then you may need to remove it or wrap it in some appropriate logic.

Related

Tabbing uiTextFields like in simulator

I am building a forms based application and i have noticed that while in the simulator using the hardware mac keyboard i am able to tab between form fields in a table using the standard tab key.
Is there a way i can call this functionality from my TextView when the didReturn method is fired? I have seen numerous threads on here with various ways to achieve something similar but they all seem overly complex and bulky using view tags for big loops which is not ideal compared to perhaps just firing a TAB keyboard command?
All you need to do is set a nextButton on your view, or inputAccessoryView for all the textFields and on selector of that button write code to make nextTextField a first responder.
By this way you can implement that tab feature. As you know there's no tab button on your iPhone ;).
You can do it via UITextField delegate method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSInteger nextTag = textField.tag+1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
[nextResponder becomeFirstResponder];
}
else {
// Do what ever want to do for last textfield
[textField resignFirstResponder];
}
return NO;
}
Set you textfields tag, sequentially & set delegate. Pressing return key in for moving to next textfield.
Hope this helps.. :)

how to check if focus switches from one text view to another

I've got 2 UITextFields. I can detect when the user in inside either of them with:
textFieldDidBeginEditing:(UITextField *)textField
Which works great. But i want the textfields to animate out when the user is not inside either of them. Currently I'm calling: (void)textFieldDidEndEditing:(UITextField *)textField
However, this is called even if i switch from on textfield to the other. Is there a better way to call do this?
What I would do is add a BOOL flag to detect if you should animate out your text fields or not. It would work something like this:
-(void)hideTextFields {
if (self.shouldHideTextFields) {
self.textField1.hidden = YES; // Or whatever you want to do with
self.textField2.hidden = YES; // your text fields
}
}
Declare a method that checks the BOOL flag and decides whether to hide or not the text fields
Whenever either of the textFieldDidBeginEditing:(UITextField *)textField methods are called set the BOOL flag (you can call it 'shouldHideTextFields') to NO.
Whenever either of the textFieldDidEndEditing:(UITextField *)textField methods are called set the BOOL flag to 'YES'. Also, call [self performSelector:#selector(hideTextFields) withObject:nil afterDelay:1]; to give the user a little time to select the other text field. If he/she does, the flag will be set to NO thanks to the above step.
Hope this helps!
On didend check if either of your textviews is currently the first responder with [textfield isFirstResponder] or [textfield isEditing]. There may be a slight delay when one ends and the other takes control. If that's the case then you could do this check after a slight delay using performSelector:afterDelay.
On textFieldDidEndEditing delegate method, do not just perform your disappearing animations, but do that in dispatch_async, checking if there is no textField editing right now.
In case if user just ended editing of one text field, there will be no editing textfield. But if user had switched to another textfield, it will already start editing and it can be easily checked by isFirstResponder method.

Using next and previous button with UITextField, problems with firstresponder

I am using a subclass of a scrollview that moves the keyboard out of the way. (TPKeyboardAvoidingScrollView)
I think this is conflicting with my implementation of the next and previous buttons. I have built an inputaccessoryview
I have a category that sets the next and previous textfields for each field
when i edit a textfield, i set the current, previous and next textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
_currentTextField = textField;
_prevTextField = [textField prevTextField];
_nextTextField = [textField nextTextField];
return YES;
}
when they click next or previous i call this method
- (void)selectAdjacentResponder:(id)sender{
UISegmentedControl *segmented = sender;
if(segmented.selectedSegmentIndex == 0){
[_prevTextField becomeFirstResponder];
} else {
[_nextTextField becomeFirstResponder];
}
}
this works fine.. but when i close the keyboard. my scrollview is messed up. if i add the line
[_currentTextField resignFirstResponder];
to the first line of my selectadjacent method it solves the problem. but the problem is it makes the screen focus in a funky way since i'm dismissing and accessing the next textfield at the same time.
i have tried resigning first responder when i close the keyboard. but i think my scrollview is calculated before that point.. any idea what to do =/

iOS - Shifting from UITextField to another doesn't start the Second One

Here is the UI:
I have a table view that has rows including text fields. For example row1 has textField1 and row2 has textField2.
Now, if the user taps the textField1, everything works fine. The delegate is called and everything.
If the user, then, taps the textField2, only the textFieldDidEndEditing: is called for textField1. textFieldDidBeginEditing: is not called for textField2 (I am pretty sure that textField2 delegate is set, because when I then tap textField2 (again), it starts editing (textFieldDidBeginEditing: is now called)
You may think this is not a real problem. The problem is that the iOS keeps showing the keyboard! With no text fields associated to it! I even loop to all text fields in all visible rows and resign them with no luck.
Seems a bug in iOS, right?
Edit: Here is the code in the delegate
- (void) textFieldDidBeginEditing:(UITextField *)textField {
[self fixTableViewOffsets];
RSMCellEditPricesCell *cell = (RSMCellEditPricesCell *) [[textField superview] superview];
[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell]
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}
- (void) textFieldDidEndEditing:(UITextField *)textField {
self.currentPriceTextField = nil;
[self fixTableViewOffsets];
[self.tableView reloadData];
}
First of all, be careful with the naming conventions, if you are adding a label at the end of a variable that variable should be a UILabel not a UITextField. Second your problem might be in the textFieldDidEndEditing method, you are calling resignFirstResponder on all your text fields(if the cell.priceLabel is a text field). You shouldn't callresignFirstResponder in textFieldDidEndEditing or textFieldSouldReturn because if those methods are called the text fields are already resign as first responder.
I couldn't find a fix for it. However, as a workaround, I disabled the second text field. When the user taps this field, it will trigger the touch event on cell, not the text field which will dismiss the keyboard. Then, I enable all text fields and the user can select this second text field again.
It's not quite a solution and it doesn't provide the best UX. Still, it work.

UITextfield becomeFirstResponder returns NO

I have implemented the standard 'next' feature on UITextFields in my app (pressing next will move you to the next appropriate text field). Most of the time this works well and will move to the appropriate textfield no problem. However on a few occasions for some reason the textField will return NO when i ask it to become the first responder and I am not sure why.
Some information about the textfields/view:
The view has multiple UITableViews, some of the UITableViewCells in these have the UITextfields i want to become first responder in them, others do not.
When the tables are created i loop through the main views subviews to find all the first responders and add them to an array of first responders (_firstResponders - variable name).
Each cell calls back via a delegate to the main view when the next responder is wanted.
Below is the code i use to get the next responder:
//get the current first responders index
NSInteger currentIndex = [_firstResponders indexOfObject:currentResponder];
// if the current index plus 1 is less than the overall count of first responders on this view then continue, else resign the responder as no more are avilable
if((currentIndex+1) < [_firstResponders count])
{
// get the potential next responder
UIResponder *nextResponder = [_firstResponders objectAtIndex:currentIndex+1];
// if it can become the first responder assign it,
// else callback into this function with the unavilable first responder
if([nextResponder canBecomeFirstResponder])
{
[currentResponder resignFirstResponder];
BOOL becomeFirstResponder = [nextResponder becomeFirstResponder];
if(becomeFirstResponder == NO)
{
DLog(#"TextField at index: %d has returned no to becoming first responder", (currentIndex+1));
}
}
else
[self getNextResponder:nextResponder];
}
else
[currentResponder resignFirstResponder];
}
As you can see i check that it is available to become the first responder in code and i have also checked that the textfield is enabled manually (I also check this at the time of adding the responder to the array of responders). It is also worth mentioning i have checked the array size and the responder to be set when this occurs and they are correct/valid.
The strangest part for me about this is that I had only previously seen this if I did not call resignFirstResponder on the current responder before setting the new responder.
Does anyone know the reason for it returning NO/how i can fix this?
Thanks in advance!

Resources