I have a strange problem with the iOS Keyboard.
In my app, I am using UITextFields inside some UITableViewCells. I want to dismiss the keyboard if the current textfield loses its focus.
This is what I've done so far:
Set up the <UITextFieldDelegate> and add [textField resignFirstResponder] to textFieldDidEndEditing:
-> textFieldDidEndEditing gets called, but the keyboard stays.
Added all TextFields to an array, looped through all objects and call resignFirstResponder
-> No effect
Called [self.tblView endEditing:YES] inside textFieldDidEndEditing.
-> Keyboard didn't disappear.
But dismissing the keyboard by using the Done-Button works perfectly (using textFieldShouldReturn)
What am I doing wrong?
Edit: I've made a video of my problem: http://www.youtube.com/watch?v=Zuz5rCv2GCo
try implementing following:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField becomeFirstResponder];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[self resignFirstResponder];
}
Related
I have a scroll view which contains a UITextField and a UITextView. The UITextField return key is "Next" and when this is pressed I call [myTextView becomeFirstResponder]; A random new line is inserted into my textview so I start on the second line. How do I avoid this?
Also important to note that this does not happen when I tap directly on the UITextView rather than tapping the next key.
Thanks in advance!
One solution to solve this is when you implement the textFieldShouldReturn: delegate method, be sure to return NO, and not YES. By returning NO, the newline isn't passed on to the text field.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// move to next field or whatever you need
[myTextView becomeFirstResponder];
return NO;
}
Okay, this behaviour is due to a bug in iOS when becoming firstresponder within same run loop by using next button. To over come this you should do this manually.
First resign first responder from a textFied, and then make textView as a first responder. like this. Implement this delegate method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[textView performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
return YES;
}
Similar idea to performSelector, but using asynchronous dispatch queue (Swift 4 version):
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
DispatchQueue.main.async {
self.myTextView.becomeFirstResponder()
}
return true
}
Before you dismiss my question as being a duplicate of this one: iphone, dismiss keyboard when touching outside of UITextField
The issue for me is that my textfield is part of my prototype cell which has its own subclass of uitableview cell and so I'm not sure how to reference the textfield when trying to resign the first responder. SO, I can't just do this:
-(void)dismissKeyboard {
[aTextField resignFirstResponder];
}
How would I get across this situation?
Thanks
Try this code, it's very useful:
-(void)touchesBegan :(NSSet *)touches withEvent:(UIEvent *)event
{
[aTextField resignFirstResponder];
[super touchesBegan:touches withEvent:event];
}
I think dismissing keyboard on tapping outside the table view cell is not good. User may accidently touching outside or he can scroll while entering text right?. You can use the UITextFieldDelegate to dissmiss keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder]
}
If I got you right you want to resign keyboard wile tapping on outSide of textfield but you don't have reference of your textfield.
Try this;
Take global textField, lets call it reftextField
Now in textFieldDidBeginEditing set referenced text field to
- (void) textFieldDidBeginEditing:(UITextField *)textField{
reftextField = textField;
}
Now you can happily use on any button clock, (adding a transparent button on begin editing recomended)
- (void)dismissKeyboard {
[reftextField resignFirstResponder];
}
Or for resigning done button try this.
//for resigning on done button
- (BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
I'm using uidatepicker pop up while click in uitextfield, The date picker is poping up with keyboard and How can I disable particular textfield keyboard and enable uidatepicker?
Firstly set your textfield delegate if you have not already set it then in delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([textField isEqual:toDateTxtFld])
{
// Add this code in delegate
[yourTextField resignFirstResponder];
// ADD YOUR CODE FOR DATEPICKER Code
return NO;
}
return YES;
}
So keyboard will resign whenever you click in textfield.
If the date picker and the keyboard are popping up together, you can always call [myTextField resignFirstResponder]; to remove the keyboard if it has popped up when the user tapped on the UITextField
In my app I have a view that is a form that has quite a few inputs.
When the UITextField calls textFieldDidBeginEditing, it checks the tag and will bring up a UIPopoverController or the keyboard depending on the what the input is meant to be.
If the keyboard is up, I need it disappear when the user presses a textfield that brings up the popover. However I cannot make it disappear, I have tried every way to get rid of the keyboard but it just stays there. I have tried:
calling resignFirstResponder in textFieldDidEndEditing
calling [self.view endEditing:YES] in textFieldDidEndEditing
calling resignFirstResponder AND [self.view endEditing:YES] in textFieldDidBeginEditing checking for the previous tag is equal to a keyboard input text field.
Any ideas would be great.
I have ripped it out and and put it in a example project if anyone wants to see the exact behaviour.
http://dl.dropbox.com/u/61692457/KB_Test.zip
Declare a Global UITextField in .h file
UITextField *txtfld;
Replace Your method textFieldDidBeginEditing with textFieldShouldBeginEditing and now write this code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField.tag == 1 || textField.tag==3)
{
if(numPickerPopover == nil)
{
numPicker = [[[NumPicker alloc] initWithStyle:UITableViewStylePlain] autorelease];
numPicker.delegate = self;
numPickerPopover = [[UIPopoverController alloc] initWithContentViewController:numPicker];
[numPickerPopover setPopoverContentSize:CGSizeMake(60.0, 260.0f)];
}
[numPickerPopover presentPopoverFromRect:textField.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[txtfld resignFirstResponder];
return NO;
}
if (textField.tag == 2)
{
txtfld = textField;
return YES;
}
return YES;
}
To dismiss the keyboard when the user touches the textField that brought it up, add this method:
- (IBAction)dismissKeyboard:(id)sender {
[textField resignFirstResponder];
}
In Interface Builder, connect this method to the textField event you want, like touch up inside (or whatever is more appropriate).
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;
}