Click here to Skip to main content
15,393,863 members
Articles / Mobile Apps
Posted 16 Dec 2012

Tagged as


5 bookmarked

Keyboard Handling in iPhone Applications using one line of code

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
23 Dec 2012CPOL2 min read
This is a tutorial about KeyboardHandler library in iPhone


Updated version of this library is available here :

Keyboard handling is a tedious task in iOS applications. As you know we have to write code to move cursor from one text field to next text field. If there are many text fields in the form than it increases the complexity.

I have developed a static library for iOS named KeyboardHandler. Using the KeyboardHandler lib you can handle keyboard in form by writing only one line of code.

I am not explaining basics of Objective c , iOS development and XCode. This tutorial is very useful for those who are familiar with iOS development.

How KeyboardHandler is useful

Handling without KeyboardHandler is difficult due to following reasons:

  1. You have to create delegate connections for each TextField or TextView in the form.
  2. You have to set ReturnKey for each TextField or TextView in the form.
  3. If there are some TextFields has keyboard type number pad than you need to add number validations.
  4. You have to override the UITextFieldDelegate and UITextViewDelegate methods.
  5. You might need to set tags.
  6. You have to done button toolbar.
  7. Then most tedious task is the following code:
    // called when textField start editing.
    - (void)textFieldDidBeginEditing:(UITextField *)textField
        activeField = textField;
        [scrollView setContentOffset:CGPointMake(0, animated:YES];
    // called when click on the retun button.
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
        NSInteger nextTag = textField.tag + 1;
        // Try to find next responder
        UIResponder *nextResponder = [textField.superview viewWithTag:nextTag];
        if (nextResponder) {
            [scrollview setContentOffset:CGPointMake(0, animated:YES];
            // Found next responder, so set it.
            [nextResponder becomeFirstResponder];
        } else {
            [scrollview setContentOffset:CGPointMake(0,0) animated:YES];
            [textField resignFirstResponder];   
            return YES;
        return NO; 
  8. Note: Above code is just sample code it may very according to developers.

KeyboardHandler completes all above 7 steps in one line of code:

[AutoScroller addAutoScrollTo:scrollView]; 

How to use KeyboardHandling lib in your project

Here are the example project:

  1. Run XCode.
  2. Click file menu and select new->project option.
  3. Select Single View Application from the templates and click next button.
  4. Name the project "KeyboardHandler Demo" and click next.
  5. Choose the location where you want to save the project and click create button.
  6. Open ViewController.xib file and add a UIScrollView in it. Now add some UITextFields as shown in snapshot.
  7. The order of textfields must match with the order with textfiellds under scrollView in objects window as shown in snapshot. I have given a number to each textfield to explain. Please see the snapshot.

    Image 1
  8. Create an IBOutlet of UIScrollView named scrollView in your ViewController.h file . Now ViewController.h will look like as follow:
    #import <UIKit/UIKit.h>
    @interface ViewController : UIViewController
        IBOutlet UIScrollView *scrollView;
  9. Open ViewController.xib and connect the UIScrollView to the IBoutlet scrollView created in above step.
  10. Now Download and extract it.
  11. Add all three files from KeyboardHandler Lib to your project.
  12. Open ViewController.m file in your project and import the header file "AutoScroller.h" .
  13. Write following line of code in viewDidLoad method of ViewController.m
      - (void)viewDidLoad
        [super viewDidLoad];
        [AutoScroller addAutoScrollTo:scrollView];    
  14. Run the application and you will see the magic of this one line of code. Whole forms handling is done by this one line.
  15. Download KeyboardHandler Demo project's source code.

Here are the full code of ViewController.h and ViewController.m files

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
    IBOutlet UIScrollView *scrollView;

#import "ViewController.h"
#import "AutoScroller.h"
@interface ViewController ()
@implementation ViewController
- (void)viewDidLoad
    [super viewDidLoad];
    [AutoScroller addAutoScrollTo:scrollView];
    // Do any additional setup after loading the view, typically from a nib.
- (void)viewDidUnload
    [scrollView release];
    scrollView = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
- (void)dealloc {
    [scrollView release];
    [super dealloc];

Points of Interest

KeyboardHandler reduces the development time. It is very important for the apps having many forms. It reduces the development time and increase the readability of code.


Currently I have tested for portrait mode only. And soon I will test for landscape and will provide updated one.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sukhpal Singh JNU Jaipur
Software Developer (Senior)
India India
B. Tech in Computer Science. Rich experience in different types of applications mainly Mobile and Web Development.

Worked on J2SE, J2EE, C, C++, C#, Objective C, Ruby, ASP.NET, Visual Basic, HTML, Java Script, CSS, Android, BlackBerry, iOS, Rhomobile and PhoneGap.

Comments and Discussions

QuestionAutoScroll with Xcode5 Pin
Member 1048211522-Dec-13 18:31
MemberMember 1048211522-Dec-13 18:31 
AnswerRe: AutoScroll with Xcode5 Pin
Sukhpal Singh JNU Jaipur12-Feb-14 0:11
MemberSukhpal Singh JNU Jaipur12-Feb-14 0:11 
GeneralMy vote of 5 Pin
vamsikrishna.neo28-Feb-13 19:29
Membervamsikrishna.neo28-Feb-13 19:29 
GeneralRe: My vote of 5 Pin
Sukhpal Singh JNU Jaipur1-Mar-13 17:22
MemberSukhpal Singh JNU Jaipur1-Mar-13 17:22 
GeneralRe: My vote of 5 Pin
Sukhpal Singh JNU Jaipur12-Feb-14 0:24
MemberSukhpal Singh JNU Jaipur12-Feb-14 0:24 
SuggestionPlease provide some feedback Pin
Sukhpal Singh JNU Jaipur18-Dec-12 6:08
MemberSukhpal Singh JNU Jaipur18-Dec-12 6:08 
GeneralMy vote of 5 Pin
gauravd.131016-Dec-12 20:01
Membergauravd.131016-Dec-12 20:01 
GeneralMy vote of 5 Pin
Sukhpal Singh JNU Jaipur16-Dec-12 18:41
MemberSukhpal Singh JNU Jaipur16-Dec-12 18:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.