hiding the keypad on iphone - 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;
}

Related

resignFirstResponder does not dismiss keyboard

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 ??

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

Handle Keyboard Done pressed event on Iphone

I am moving my view when a text field is pressed in order to get proper view when keyboard appears. Now, when the Done keyboard button is pressed, I would like to return the view to its initial state. How do I handle an action when the done keyboard button is pressed?
The proper way to do this is to observe to the notifications UIKeyboardDidShowNotification and UIKeyboardDidHideNotification as detailed in Apple's documentation.
If you want to know when the Done button has been pressed, implement
- (BOOL)textFieldShouldReturn:(UITextField *)textField
In your delegate. It should be called when the return button is pressed. See API documentation for more details.
Make an IBAction and connect it to the text field's didEndOnExit method. Then within the implementation of this method you should put [yourTextFieldOutlet resignFirstResponder];, which will deactivate the text field.
#freespace has it right, this is all you need to do.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
Tap the done button and poof, the keyboard is gone.
If you just want to know that Done was pressed, you can ask to be told of that control event:
[textField addTarget:self
action:#selector(donePressed)
forControlEvents:UIControlEventEditingDidEndOnExit];
That's the code version of IB's didEndOnExit.

How to make the keyboard go away?

I added a text field, and when I tap in there, the keyboard slides up. What are the steps needed that the keyboard shows an "OK" which actually really works? I mean...hitting "OK" or "Return" does exactly nothing.
Is there some sort of KeyboardDelegateProtocol where I must do some weird stuff like
-(BOOL)shouldReallyGoAway {
return YES;//of course!
}
Is there an 200 pages keyboard programming guide to read? Couldn't find one...
You need to handle the tap on the 'OK' button (Did End On Exit event) and resign the first responder status for the text field.
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
You might want to handle few other actions - like the background tap. You'll need an invisible button that will cover your background and trap the taps and an IBOutlet for your text field (since your sender will be the button, not the text field)
No reason for a 200 pages guide, it's right there in the introduction of the UITextField which you propably use.
Use
[aTextField resignFirstResponder];
to make the keyboard go away programmatically.
Also, there is the UITextFieldDelegate with appropriate methods regarding Return, which you might use, i.e. -textFieldShouldReturn:.
e.g.
- (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.