Click here to Skip to main content
Click here to Skip to main content

Keyboard Handling in iPhone Applications using one line of code

, 23 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
    // 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:

[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.

  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;
        
    }
    @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
      - (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;
}
@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)

Share

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 PinmemberMember 1048211522-Dec-13 19:31 
AnswerRe: AutoScroll with Xcode5 PinmemberSukhpal Singh JNU Jaipur12-Feb-14 1:11 
GeneralMy vote of 5 Pinmembervamsikrishna.neo28-Feb-13 20:29 
GeneralRe: My vote of 5 PinmemberSukhpal Singh JNU Jaipur1-Mar-13 18:22 
GeneralRe: My vote of 5 PinmemberSukhpal Singh JNU Jaipur12-Feb-14 1:24 
SuggestionPlease provide some feedback PinmemberSukhpal Singh JNU Jaipur18-Dec-12 7:08 
GeneralMy vote of 5 Pinmembergauravd.131016-Dec-12 21:01 
GeneralMy vote of 5 PinmemberSukhpal Singh JNU Jaipur16-Dec-12 19:41 
Outstanding works fine...

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411019.1 | Last Updated 23 Dec 2012
Article Copyright 2012 by Sukhpal Singh JNU Jaipur
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid