Home > DeveloperSection > Articles > Sample on Hiding Keyboard in iOS

Sample on Hiding Keyboard in iOS


iPhone iPhone  Ios  Objective C 
Ratings:
0 Comment(s)
 771  View(s)
Rate this:

Sample on Hiding Keyboard in iOS



In iPhone/iPad touch devices whenever the user want to enter the text data into the text input view like text field then as a result on touching text field a keyboard automatically appears on the screen, this keyboard does not automatically go away after the user finished typing.

 

As an experience if you see other iPhone apps you can see that when we press Return key on the keyboard or tapping anywhere on the background of the user interface keyboard will recede from the view. To recede the keyboard from the view developer write some code to enable the functionality. So here we will learn how we can enable hide keyboard functionality in our sample application. 

 For creating HideKeyboard application follow the steps:

 

1.   Start the Xcode application, goto File > New > Project > select 'Single View Application' and click on Next button. After that provide the product name 'HideKeyboardViewController', provide the matching prefix. Select iPhone in Devices. Uncheck the 'Storyboards' and CheckMark the 'Automatic Reference Counting' and click on 'Next' button. Now select the location and click on 'Create' button, ok our project is created, this is the hierarchy of our project.

 

      

 

2.   Now, select the MainStoryboard.storyboard file and drag the UITextField from the Object Library panel on the view area (View > Utilities > Show Object Library). Like this:

 

 

3.   Now, we need to create an outlet for the UITextField in our HideKeyboardViewController.h file. Select 'Assistant Editor' on the top-right in the Xcode, two view will be open in first view open the HideKeyboardViewController.xib file and in second view open HideKeyboardViewController.h file. Now press Ctrl and left click on the text field and drag it under the @interface and name it as textField after that an outlet of UITextField would be created, and also create a reference variable of UITextField. Like this:

@interface HideKeyboardViewController : UIViewController

{

   UITextField *textField;

}

@property (strong, nonatomic) IBOutlet UITextField *textField;

 

4.   Now, we need to synthesize the textField in the implementation file HideKeyboardViewController.m. Like this:

@implementation HideKeyboardViewController

@synthesize textField;

.

.

.

@end

 

5.   Now, to hide the keyboard when the user touches the Return key of the keyboard we need to write a method that will be responsible to hide the keyboard. Open the HideKeyboardViewController.h file and declare textFieldReturn: method between the @interface and @end. Like this:

@property (strong, nonatomic) IBOutlet UITextField *textField;

-(IBAction)textFieldReturn:(id)sender;

 

 

6.   Now, we need to declare textFieldReturn: method in the implementation file, and write a line of code, like this: 

-(IBAction)textFieldReturn:(id)sender

{

   [sender resignFirstResponder];

}

             in the above code resignFirstResponder: method is responsible for hiding the keyboard.

 

7.   Now, select the text field in the HideKeyboardViewController.xib file and display the connection inspector in the right hand panel and click on circle 'Did End on Exit' event and drag a line on the text field and select the textFieldReturn from the list of available methods.

 

 

8.   Now, to hide the keyboard when the user taps on the background view of the screen. We need to write another method backgroundTouched: in the HideKeyboardViewController.h file. Like this:

     - (IBAction)backgroundTouched:(id)sender;

 

9.   and in the implementation file HideKeyboardViewController.m implement the action by calling resignFirstResponder: method. Like this:

 

-(IBAction)backgroundTouched:(id)sender

{

   [textField resignFirstResponder];

}

For our backgroundTouched: action method we need to make sure that it gets called when the user touches the background view. So select the MainStoryboard.storyboard view and display the Identity Inspector from the right hand panel and change the custom class UIView to UIControl. Now display the connection inspector and click on the circle to the right of the Touch Down event and drag the connection line to the UITextField icon and release the mouse and select the backgroundTouched: method from the resulting list. (Screen shot):

 

 

Here is our complete Source Code: 

HideKeyboardViewController.h file

#import <UIKit/UIKit.h>

@interface HideKeyboardViewController : UIViewController

{

UITextField *textField;

}

@property (strong, nonatomic) IBOutlet UITextField *textField;

-(IBAction)textFieldReturn:(id)sender;

- (IBAction)backgroundTouched:(id)sender;

@end

HideKeyboardViewController.m file 

#import "HideKeyboardViewController.h"

@interface HideKeyboardViewController ()

@end 

@implementation HideKeyboardViewController

@synthesize textField;

-(IBAction)textFieldReturn:(id)sender

{

[sender resignFirstResponder];

}

-(IBAction)backgroundTouched:(id)sender

{

[textField resignFirstResponder];

}

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

Screen Shots: 

   

 


Don't want to miss updates? Please click the below button!

Follow MindStick