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

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.

Related

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.

What does the return value mean for the UI TextField Delegate method textFieldShouldReturn:?

The apple docs offer:
Asks the delegate if the text field should process the pressing of the
return button.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Parameters textField The text field whose return button was pressed.
Return Value YES if the text field should implement its default
behavior for the return button; otherwise, NO.
Discussion The text field callsthis method whenever the user tapsthe
return button. You can use this method to implement any custom
behavior when the button is tapped.
My question is what does the return value do? I have been implementing the behavior in this method so it makes no difference what is returned. Is this not the correct method to perform the action?
For instance, if I implement a search function, should I trigger the search action in this method or somewhere else.
This is the correct method to trigger an action when the user taps the Return keyboard key (whatever it happens to be labeled).
The return value from the textFieldShouldReturn: delegate method almost never matters. If you are dealing with a single text field then it definitely doesn't matter.
I ran into one issue a while back that made me realize that just under the right situation, the return value does matter. I had a screen with several text fields and then a text view. I was using this text field delegate method to change the first responder from text field to text field to text view. I found that if I returned YES in this delegate method and then made the text view the first responder, the newline was being sent to the text view.
As a result of this, I now always return NO from this delegate method to be safe.
When you press the return button on the keyboard the textFieldShouldReturn is called.
I never experienced any difference between the return value.
Customization example:
If you have two textFields when the user presses return button from first textField, you can give focus to second field in the following way:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(#"textFieldShouldReturn:");
if (textField.tag == 1)
{
UITextField *passwordTextField = (UITextField *)[self.view viewWithTag:2];
[passwordTextField becomeFirstResponder];
}
else
{
[textField resignFirstResponder];
}
return YES;
}
So you can use this delegate method for triggering search functionality.

UItableview and Keyboard with previous and next button scrolling issue - iPhone

Here I have a tableView on a scrollView. Each row contains a textField. Next button is ok(view animating properly on upword) but when I press on previous button on keyboard, I have to show down the view one by one according to the textField. How can I solve this? I have to do it in many views. Please help me as soon as possible. I badly need it. May be I'm doing wrong somewhere. Sorry for my language problem. Sample code may be helpful.
Hi Please try the below method to scroll your tableview up and down based on which text field is selected, write this code in textfield delegate method
- (void)textFieldDidEndEditing:(UITextField *)textField{
// calculate the nsindexpath based on the textfield which is selected
//- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
[yourtableview scrollToRowAtIndexPath:calcutaedIndexPath atScrollPosition:UIScrollViewPositionTop animated:YES];
}
Hope it will help you

stop keyboard dismissing when switching uitextf

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.

Remove keyboard when table cell selected

I have a modal set up like this:
Despite appearances, the first two fields are UITextFields that become first responders and display the keyboard. The third 'Department' item is a table view and pushes another view.
I have implemented a scroll view so that while either of the fields is being edited, the user can scroll around and reach the department cell:
When it is selected, before the push, I would like to hide the keyboard. It's a small detail, but try adding a new event on Apple's 'Calendar' app. It opens with the first text field as first responder (so keyboard is present). If you select the start/end cell, the keyboard hides as the next view is pushed.
How do I achieve this? As a test I tried:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"hiding keyboard");
if ([ingredientName isFirstResponder]) {
NSLog(#"resigning name field");
[ingredientName resignFirstResponder];
}
else if ([ingredientAmount isFirstResponder]){
NSLog(#"resigning amount field");
[ingredientAmount resignFirstResponder];
}
}
The logging confirms that these are getting called. But the keyboard does not hide. It simply stays in place as the view slides out. And on return, the previous field still has focus (and the keyboard is out).
Any ideas?
resignFrstResponder will not dismiss the keyboard when you're using a modal view.
Another dev found a workaround for this behavior here: http://viraj-workstuff.blogspot.com/2010/12/resignfirstresponder-does-not-hide.html
Not terribly elegant, but try stealing the firstresponder status with an ad-hoc, invisible UITextField:
-(void)dismissKeyboard {
UITextField *textField;
textField=[[UITextField alloc] initWithFrame:CGRectZero];
[self.view addSubview:textField];
[textField becomeFirstResponder];
[textField resignFirstResponder];
[textField removeFromSuperview];
// [textField release] // uncomment if not using ARC
}
Two things:
Are you using a segue for pushing that other VC? If so, resign the keyboard in the prepareForSegue method.
If not, try using willSelectRowForIndexPath instead of didSelectRowAtIndexPath

Resources