Tell any UITextField to dismiss - iphone

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) {...}

Related

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;
}

Hiding keyboard when clear button is pressed in UITextField

Is there any way to hide the keyboard when a clear button of UITextField is pressed?
Yes, there is, although I suspect that doing so would violate the Apple Human Interface Guidelines.
To do so, add the following method to your view controller's implementation file. Then make the view controller into your textfield's delegate.
- (BOOL) textFieldShouldClear:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
The downside to this approach is if you ever want to prevent the textfield from clearing, your code becomes messy. Instead you might try to define a custom method and then connect it to the valueDidChange method and check for an empty value.
-(IBAction)hideKeyboardFromTextField:(id)sender{
//TODO: Check if the previous value was longer than one character to differentiate
//between backspace and clear.
//check if the editing caused the box to be empty
if([[sender value] isEqualToString:#""] || [sender value] == nil)
[sender resignFirstResponder];
}
}
The problem here is that you can't easily differentiate between a tap on the clear button and a tap on the delete button when there is one character in the UITextField.
As I said in the beginning of my answer, this is not advisable in the first place and as the answers here have shown, it is not so easy to implement. I don't think it's worth the hassle, considering the difficulty involved and the fact that it doesn't result in optimal user experience.
This code is definitely working for me to hide the key board while clearing out the content of the textfield
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = #"";
return NO;
}
Yep. Call resignFirstResponder on the text field in the delegate's textFieldShouldClear: method.
In UITextFieldDelegate
- (BOOL)textFieldShouldClear:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
But there is a problem with this. From the manual, "The text field calls this method in response to the user pressing the built-in clear button. (This button is not shown by default but can be enabled by changing the value in the clearButtonMode property of the text field.) This method is also called when editing begins and the clearsOnBeginEditing property of the text field is set to YES."
Note that, this method is called when editing begins if clearsOnBeginEditing is set to YES. So if you call resignFirstResponder in this method then editing will not begin actually. So you need to set clearsOnBeginEditing to NO. Obviously then the text field won't be cleared when editing begins.
Another IMPORTANT matter not directly related to the question. Hiding the keypad after tapping clear button is not a familiar behavior and Apple does NOT like changing the behavior of standard items. You may get a rejection for this.
Try this code:
[TextField performSelector:#selector(resignFirstResponder) withObject:nil afterDelay:0.1];
For Swift
In your UITextFieldDelegate
func textFieldShouldClear(_ textField: UITextField) -> Bool {
textField.resignFirstResponse()
return true
}

Unable to dismiss keyboard with UITextField

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

hiding the keypad on iphone

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;
}

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.