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.
Related
I am doing an application and I need to detect when a UITextField is clicked. I tried using touchesBegan but it doesn't react to textfield when clicked, only outside of it. I am only starting with Objective-C so if you give me advice, please let it be detailed. Thank you.
You can try the delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// Do something
}
As long as the text field has interaction enabled and is editable. However, a second tap will not be detected if the text field is already the first responder.
Set the textview's delegate property to an object that overloads the textViewShouldBeginEditing: function. You could also use the textFieldDidBeginEditing: function of the same delegate.
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.
i have app i want that when user enters any data and text field has by default 10 so when user click on textfield then it clear the textfield to enter data.It mean when user enters data it doest not delete one bye one like clear 1 and o with back space but by default when user click textfield it should get clear.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textfield settext:#""];
}
if you focus on textfield then this method is called and textfield is cleared.
Apple Developers already did this for you, set the 10 into UITextField placeholder property see doc, when you tap on the textfield, it will be remove automatically!
However, if you want to clear it in case you've added 10 as text of textfield then,
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.text = #"";
}
It will call each time when you focus to textfield.
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.
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.