Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile

Keyboard Handling in iPhone Applications using one line of code

5.00/5 (4 votes)
23 Dec 2012CPOL2 min read 29.8K   577  
This is a tutorial about KeyboardHandler library in iPhone

Introduction 

Updated version of this library is available here : http://www.codeproject.com/Tips/514396/Keyboard-Handling-in-iPhone-Applications-Part-2-2

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:
    C++
    // called when textField start editing.
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        activeField = textField;
        [scrollView setContentOffset:CGPointMake(0,textField.center.y-60) 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,textField.center.y-60) 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:

C++
[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:
    C++
    #import <UIKit/UIKit.h>
     
    @interface ViewController : UIViewController
    {
        IBOutlet UIScrollView *scrollView;
        
    }
    @end 
  9. Open ViewController.xib and connect the UIScrollView to the IBoutlet scrollView created in above step.
  10. Now Download KeyboardHandler_Lib.zip 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
    C++
      - (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

C++
#import <UIKit/UIKit.h>
 
@interface ViewController : UIViewController
{
    IBOutlet UIScrollView *scrollView;
}
@end 

#import "ViewController.h"
#import "AutoScroller.h"
@interface ViewController ()
 
@end
 
@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];
}
@end  

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.

History

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

License

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