I have a UITextField and a UIButton. If the button is tapped while the text field is active, I want to dismiss the keyboard, but I'm unable to do so. I tried calling [textField resignFirstResponder] and [textField endEditing:YES] when the button is tapped, but they have no effect -- [textField isFirstResponder] returns false, so it's not surprising that resignFirstResponder has no effect. But in that case, why isn't the keyboard disappearing? Thanks.
Make sure your "textField" class member is properly hooked up in the nib file. NSLog it's value right before you call firstResponder and see if it's nil. Also, enter text into the textfield and see that you can properly print it out with NSLog at the same point you're calling resignFirstResponder. Usually when a control does not respond correctly it's because IBOutlets aren't hooked up properly.
Try making an IBAction
-(IBAction)textFieldReturn:(id)sender
{
[sender resignFirstResponder];
}
And connect it to the UIButton
have you added <UITextFieldDelegate> in your .H ??
Related
I have a very weird bug here, this is the scenario;
-I click on one of the textfields on my UITableView, uikeyboard appears,
-Then without pressing done, I click another textfield on screen(a disabled one),
-This disabled textfield pushes a detailview controller,
-I do my job on the detailview and return back,
-Surprize! the main UITableview is no more scrollable to bottom anymore! so I can not edit the bottom cells anymore!
This does not happen IF I press done after editing(so close the keyboard) and then click the detail view, now it works good.
I think it is something about resignfirstResponder is not called before I switch to another view, so I tried to send this msg to all the textfields in the tableview..but it got worse.
I tried also adding this line below just before I push the detailviewcontroller, but not worked, this is interssting cause this is what I call to resign the number pad normally, and it works when it is called from my custom "done" button to dismiss the nuber pad
[[self view] endEditing:YES];
And this is how I set the uikeyboards depend on the content in cellforRowAtIndex method
if(somelogic){
cell.textField.keyboardType=UIKeyboardTypeNumberPad;
[self resignFirstResponder];
[self becomeFirstResponder];
}
else{
cell.textField.keyboardType=UIKeyboardTypeDefault;
[self resignFirstResponder];
[self becomeFirstResponder];
}
Any Ideas?
Just call [currentTextField resignFirstResponder] before you push the detail view controller.
Set the currentTextField in the following method.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentTextField = textField;
// Your code here
}
Occam's razor.
Don't let them not click the done button.
Put a transparent UIButton accross the entire screen. Show it when you call [textField becomeFirstResponder]. You can either prevent people from touching the screen and switching textFields, or call [textField resignFirstResponder] when it is tapped.
I create a uitextfield and explicitly I call:
[myTextField setEnabled:YES];
[myTextField becomeFirstResponder];
but the keyboard isn't showed. Can I explicitly call the keyboard?
Thanks
have you checked if your textfield is properly initialized ? Or is it for some reason nil.
or you did not connect it properly in IB ?
Maybe you can provide more information ?
this is possible if the user has an external keyboard enabled. You CANNOT explicitly call the keyboard
For some reason, resignFirstResponder is not working. I am not sure why? I have tried to call it from textFieldDidEndEditing and nothing happens. A NIB is being used and each's delegate is pointing to files owner.
What needs to be done to get the keyboard to dismiss?
Thanks.
Don't use -textFieldDidEndEditing. That's called after the text field resigns firstResponder status, which is what you're trying to use it as a hook to make happen. Cart before horse, chicken-and-egg kind of problem.
Instead use -textFieldShouldReturn to get triggered when the return key is pressed (and remember to return YES; from that.) Also float a clear custom button behind the elements of the view and handle a "background tap" that goes through all the text fields on your view and resigns first responder on the lot of them.
actually you should return NO so that the text field does not begin editing at all. If it does, the firstresponder gets set and the keyboard pops up again.
Make sure your setting your delegates for the textfield.
myTextField.delegTe = self;
And you are using in your header:
<UITextFieldDelegate>
EDIT:
Try:
if(textField == myTextField){
[textField resignFirstResponder];
}
I cannot get textFieldShouldEndEditing to call. I have re created the links in interface builder and tried, but nothing seems to work. Any idea why this would not be called?
Edit
I am implementing both -textFieldShouldEndEditing and -textFieldDidFinishEditing. Adding the delegate for the textField didnt help either. The code I am using below is not triggering the log messages.
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(#"Done editing...");
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(#"Done editing...");
}
Don't use -textFieldDidEndEditing. That gets called after the text field resigns first responder... which it hasn't yet because you haven't told it to because that method hasn't been called yet because first responder hasn't been resigned because you haven't told it to yet. Circular, chicken-and-egg sort of problem.
The delegate method you want is -textFieldShouldReturn. That gets called when the "return" key is pressed. Inside that, tell the text field to resign first responder, then return YES.
Also put a big clear custom button behind your form, and hook its touch-up-inside to a method that goes through all the text fields in the form and resigns first responder on each of them. So a background touch releases the keyboard too.
I saw it was a long-long time ago and I hope that since then you could solve the problem. Everybody tells you the right thing, but you have to take care of all at the same time! Let me just go through on all of them:
Make the promise first:
#interface yourViewController : UIViewController <UITextFieldDelegate>
Then make sure, you also hook up your textfields' delegates on the storyboard to the actual viewcontroller (yourViewController)!
When its done, implement the method in the right viewcontroller file (yourViewController.m):
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Test it, and it should work now...
I hope it helps!
set the delegate to your TextField
In the interface Builder select your TextField, then press command + 2(Connections) and connect the delegate outled with the file's owner then save, and with that should work.
the delegate method are:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
}
Hope this can help you.
Have you set the delegate?
If not,set using
textField.delegate=self;
or else,
You must include UITextFieldDelegate in .h file
#interface yourViewController : UIViewController <UITextFieldDelegate>
EDIT:
You have to code in this delegate textFieldDidEndEditing not this textFieldShouldEndEditing
I have a form that looks like the following (see image). If the user hits the Login button, I want the keypad to disappear. How do I do that.
Note that TextFieldDelegate methods wouldnt get called since the user is simply hitting the UIButton (Login). Hence, anything I can put in the IBAction for this button?
Normally the keyboard should be dismissed automatically when the user taps somewhere outside of the textfield, but you can also manually hide it using
[textField resignFirstResponder]
set delegate for your text field <UITextFieldDelegate>
and over ride this method
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}