Ted pottel
Ted pottel

Reputation: 6993

How can I make the keyboard go away?

I have a app I'm making that has 2 text fields and buttons below it. When the user starts to type, the keyboard comes up and covers the buttons. Is there a way to make the keyboard go away?

Upvotes: 0

Views: 66

Answers (3)

chewy
chewy

Reputation: 8267

There are two ways to do so

// this one line will dismiss the keyboard from anywhere in your code

[self.view endEditing:YES];

or

with the delegate of textfield (you should declare it in your .h file first)

@interface someController : UIViewController <UITextFieldDelegate>

and the in .m

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

   // next line will dismiss the keyboard 
   [textField resignFirstResponder];

    return YES;
}

Upvotes: 1

William Entriken
William Entriken

Reputation: 39333

I use this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self.alertView dismissWithClickedButtonIndex:self.alertView.firstOtherButtonIndex animated:YES];
    return YES;
}

In the view controller after setting the text field delegate to self.

Upvotes: 1

zrzka
zrzka

Reputation: 21249

Check UITextField documentation:

To dismiss the keyboard, send the resignFirstResponder message to the text field that is currently the first responder. Doing so causes the text field object to end the current editing session (with the delegate object’s consent) and hide the keyboard.

In other words, just send resignFirstResponder to an active text field object.

Upvotes: 1

Related Questions