Unable to dismiss keyboard with UITextField - iphone

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

Related

How to identify which textfield is currently first responder

If you have several text fields on the screen, and the keyboard pops up for each one when they are tapped, what if you want to programmatically hide the keyboard, or resign first responder, but you don't know which textfield to send the resignFirstResponder message to? Is there a way to identify which object/textField should get this message?
check all of your textfield call
[textfield isFirstResponder]
You could keep track of which text field is the first responder by either setting your view controller to be the delegate object of all text fields and then when your subclassed text fields gets the "becomeFirstResponder" method call, tell your view controller which text field is the current one.
Or, there's a more blunt force approach which is a category extension to "UIView":
#implementation UIView (FindAndResignFirstResponder)
- (BOOL)findAndResignFirstResponder
{
if (self.isFirstResponder) {
[self resignFirstResponder];
return YES;
}
for (UIView *subView in self.subviews) {
if ([subView findAndResignFirstResponder])
return YES;
}
return NO;
}
#end
which I got from this potentially very related question.
You Can Check Your all TextField and than Identify Easily.
[textfield isFirstResponder];
There is no public method to get the current first responder, but you can do things to still get it; The first one is obviously to keep track of this yourself. You can do this in various way and if you don't want to touch any existing class but just want it to work, a category and method swizzling will do the trick. The more cleaner solution however is to iterate through the view hierarchy and ask the views wether they are the current first responder. You can start with the root UIWindow and start iterating, or you can start with your current UIViewController's view, but keep in mind that the current first responder doesn't have to be part of your roots UIWindow view hierarchy (eg. if you have a text field inside an UIAlertView).
Try this (Swift 3):
if textField.isEditing {
textField.resingFirstResponder()
}

backgroundTouched not working in uitableviewcontroller iphone

my code:
.h:
- (IBAction)backgroundTouched:(id)sender;
.m
- (IBAction)backgroundTouched:(id)sender {
[textField resignFirstResponder];
[self.view endEditing:YES];
NSLog(#"backgound taped");
}
I dont know why the backgroundTouched not called when i tap the background and the keyboard not hidden. I think never called because of wiring up code problem.
Neither Both textField resignFirstResponder and self.view endEditing:YES nor NSLog is working.
Can anyone let me know how to do this? or What am i missing here?
Im trying to hide the keyboard after done writing in uitextfield, the textfield is inside uitableview cell.
P.S i made it in uitableviewcontroller without xib file
Thank you.
Instead of a backgroundTouched method (which I have no idea how it gets actually called), consider using the delegate method for UITextField, namely textFieldDidEndEditing: (I've linked Apple's documentation for you).

How to identify UITextFields and Keyboard Activity?

I've searched in the web but didn't found anything similar to what I want. So,I am creating an application and I need to recognize when the user leaves a specific UITextField,more clearly.when the user enters a value in the UITextField and after touch outside to dismiss the keyboard, I need to recognize that the UITextField has lost activity for,after I perform an action.
Is this possible?
Look up UITextFieldDelegate in the Apple docs. Specifically the methods textFieldDidEndEditing: and textFieldShouldReturn:. Hook up the specific UITextField to an outlet and assign its delegate to your viewController. Then in the delegate method, if you need to make sure it's a specific text field, compare it to the IBOutlet.
write UITextFieldDelegate in .h file then after include the following method in .m file.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}

Tell any UITextField to dismiss

How can I tell the current firstResponder to resign? I have a UITableView with a bunch of TextFields and I don't know which is active at all times. I thought about storing pointers to all cells in an array and iterating through it, telling every cell to resignFirstResponder but I'm sure there is an easier way. Maybe something like [CurrentFirstResponder resignFirstResponder]?
I would appreciate some help, Fabian
EDIT: I don't want to dismiss the keyboard when the user taps done. It should be dismissed programmatically. Since I don't know which UITextField is active at any time, I am searching for something that calls resignFirstResponder on the current FirstResponder.
You could keep a reference to the UITextfeild that's actively editing using textFieldDidBeginEditing: on the UITextFieldDelegate Protocol or you could do this with your parent view:
UIView * myParentViewView;//view containing one or more editable UI controls
[myParentViewView endEditing:YES];
I hope this will solve your problem,
Assign delegate to UItextField,
textField.delegate=self;
then in following method
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//This for to resign on begin editing
[textField resignFirstResponder];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//This for to resign on end editing
[textField resignFirstResponder];
}
If you dont want to the textField to be editable then,
textField.editing=NO;
Set tag to distingush your textFields
Simply use the UITextFieldDelegate (reference). Whenever - (BOOL)textFieldShouldReturn:(UITextField *)textField is called, perform [textField resignFirstResponder], since this method is always invoked with the currently active textfield.
If you still need to distinguish between your textfields, try setting a tag and use it with if(textfield.tag == self.mytextfield.tag) {...}

How to make the keyboard go away in the iphone simulator

I've got one text entry box in my iphone app, when you touch it in the simulator, the keyboard pops up. But there's no way to get rid of it.
Other web pages give solutions, without explaining why they should work, and they don't work for me.
One says make the text box's delegate your uiview then call resignfirstresponder on the object, but it never gets called.
Any suggestions? Can anybody explain what's actually going on? I can figure it out myself if I knew what the design paradigm was...
Maybe I should just put a "go" button so I have something to get the focus away from the textfield?
One way to do it is to set an object as the delegate to the text field and implement
- (BOOL)textFieldShouldReturn:(UITextField *)textField
in which you call [textField resignFirstResponder]
This will cause the keyboard to disappear when they push the return/go/done button (whatever you set the bottom right keyboard key to be).
See the UITextFieldDelegate reference for more info.
To dismiss keyboard you can use TextField Delegate.
To use this follow these steps...
1. In you viewController.h add the delegate declaration like this:
#interface ViewController : UIViewController <UITextFieldDelegate> {
}
2. In your viewController.m call this method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
3. Then write a code like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
4. Final step is to set the textField's delegate:
- (void)viewDidLoad {
[super viewDidLoad];
self.textField.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField in your delegate is called when the return button is pressed.
You want to call [textField resignFirstResponder] and then return YES. That should do the trick. Also make sure the delegate is set. If in doubt, add a break point or NSLog and verify.
You can simply type the following:
textfield.endEditing(true)
Where textfield is the name of your UITextField.