Dismis keyboard on UITextField return key pressed


Let you have a text filed and a button in a view. if you pressed the button something will  happen.

Now if you click or select the text field a keyboard will appear. now if you press the return key of the keyboard this  will not disappear. If   you want to make it disappear in the time of clicking on the button just add the following code in the action function of the button.

[self.inputTextField resignFirstResponder];

where inputTextField is the object which is mapped with the textfield of the view.

Now when you click on the button keyboard will disappear.

Now if you want to enable return button of the keyboard (keyboard will disappear when return key will be pressed) you have to do the following.

edit the viewController header(.h) file. Specify the textfield deligate after the declaration of interface as bellow:
@interface DialerViewController : UIViewController< UITextFieldDelegate>
Now add the following function to your viewController.m file
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField==self.textFieldInput)
{
[self.textFieldInput resignFirstResponder];
return NO;
}
return YES;
}

your textFiled keyboard type should be UIKeyboardTypeNumbersAndPunctuation or any type that shows return button in keyboar view. so when you click on the return button this function will be called.

Now what if you want to show only nuberpad and what has no return button. Then how you dismiss or hide the keypad? In my next post I will show how to do that.

Thanks.

About these ads

About Razib Chandra Deb

I completed my BSc in CSE from CSE Department of KUET. Currently I am working as a Software Engineer in Samsung R&D Institute Bangladesh(SRBD). I am interested in image processing.
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

3 Responses to Dismis keyboard on UITextField return key pressed

  1. cgchinmay says:

    How can we use TextField Delegate method without specifying the Delegate protocol?we should get errors but we don’t.

  2. Hi there, just wanted to mention, I loved this article. It
    was funny. Keep on posting!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s