Show Menu

Looking to hire an app developer?

Submit your 30 day Job Listing for FREE

iOS development tutorial

When you are dealing with UITextFields one thing you need to handle is the showing and hiding of the keyboard. In this Objective-C Tutorial I will show you how to correctly manage the keyboard for UITextFields.

To show the keyboard all you need to do is make your text field the first responder like this:

[self.myTextField becomeFirstResponder];

If you want the keyboard to be there when your view appears on the screen then add this line of code in the viewDidAppear: rather than the viewWillAppear: method.

If you want to hide the keyboard then all you need to do is resign being the first responder.

[self.myTextField resignFirstResponder];

If you want the keyboard to hide when the user taps outside of the text field the touchesBegan:withEvent: method is a good place to resign first responder.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.myTextField resignFirstResponder];

If you want the keyboard to hide when you press the return or done button of the keyboard then you need to make sure that the view controller is the delegate for your text field and resign first responder in the textFieldShouldReturn: method.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.myTextField resignFirstResponder];
    return NO;

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly

Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.

Written by:

Vasilica Costescu is just a girl that loves her iPhone and iOS programming, she graduated from: West University of Timisoara with a degree in Computer Science in 2006. Her blog has awesome articles. Check it out.