Click here to Skip to main content
11,649,582 members (80,077 online)
Click here to Skip to main content

Keyboard Handling in iPhone Applications: Part 2/2

, 14 Jan 2013 CPOL 10.2K 273 2
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Keyboard Handling in iPhone Applications"

Update

As there were some bugs with the previous library, I have updated the lib and attached a sample project. You may find the latest lib in the attached sample project. The fixes are:

  1. Fixed the SIGABRT while launch on XCode 4.5 and XCode 5.
  2. New library is universal. It works on both simulator and device.
  3. Fixed the SIGABRT unable to load nib "doneToolbar.xib" .
  4. I have placed doneToolbar.xib in KeyboardHandlerLib/include/AutoScroller folder, please check in the attached sample project.  

Introduction

I have posted a tutorial about keyboard handling here. As I mentioned, there were some limitations of the KeyboardHandlerlibrary. Here, I am updating and providing you with a new sample project and lib files.

What is Added to this New Version

As I mentioned above, I already posted a tutorial here. That's why I am not explaining here how to use it. You have to read the previous tutorial. Here, I am explaining what is extra in this version.

In the previous version, there were some limitations:

  1. All form fields must be added directly to the UIScrollView. So in case you have a UIViewcontaining some TextFieldsand this UIViewis added to the UIScrollView, then the previous version of lib will not work properly.
  2. That was compatible only with portrait mode.
  3. If there are some hidden fields in the form, then the previous version will not work perfectly.
  4. Was not tested for iPad applications.

What is in the new version:

  1. Works for each type of forms whether there are subviews in scrollview. Please see the sample application.
  2. Works for both landscape and portrait.
  3. Hidden fields are skipped.
  4. Compatible with iPhone and iPad applications.

How to Use It in Your Project

Here is 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. Choose the "universal" type.
  5. Name the project "LibTest" and click next.
  6. Choose the location where you want to save the project and click create button.
  7. Open ViewController.xib file and add a UIScrollViewin it. Now add some UITextFieldsas shown in snapshot.
  8. The order of textfieldsmust match with the order with textfieldsunder scrollViewin objects window as shown in the snapshot. I have given a number to each textfieldto explain. Please see the snapshot.

  1. Open ViewController.xib and connect the UIScrollViewto the IBoutlet scrollViewcreated in the above step.
  2. Now download KeyboardHandler_Lib_v1.0.1___.zip and extract it.
  3. Add all three files from KeyboardHandlerLib to your project.
  4. Open ViewController.m file in your project and import the header file "AutoScroller.h" .
  5. Write the following line of code in viewDidLoadmethod of ViewController.m.
  6. - (void)viewDidLoad
    {
        [super viewDidLoad];
        [AutoScroller addAutoScrollTo:scrollView];    
    }
  7. Run the application and you will see the magic of this one line of code. Whole forms handling is done by this one line.
  8. Portrait will look like follows:
  9. And Landscape snapshot will look like:
  10. Download LibTest_iPhone_and_iPad_1.zip project's source code.

Here is 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

Other Important Methods of AutoScroller Class

  1. +(AutoScroller*) addAutoScrollTo:(UIScrollView*)scroll;
  2. @pararmeters

    • scroll: Instance of UIScrollViewcontaining form fields.
    • You should use this method in case you don't want to use delegate methods of UITextFieldsand UITextViews. It will show Done Toolbar on the keyboard for each form field.

  3. +(AutoScroller*) addAutoScrollTo:(UIScrollView*)scroll:(BOOL)showDoneButtonToolbar:(id<UITextFieldDelegate>)textFieldsDelegate;
  4. @parameters:

    • showDoneToolbar: If passed NO then Done Toolbar only will be added to the UITextViewtype fields. Otherwise on all fields.
    • textFieldsDelegate: Pass a common UITextFieldDelegate for all UITextFields in the form. You must not set delegates explicitly (such as textField.delegate=self) and must not make delegate connections through interface builder.

    This method is useful when you want to use delegates of the UITextFields.

  5. +(AutoScroller*) addAutoScrollTo:(UIScrollView*)scroll:(BOOL)showDoneButtonToolbar:(
      id<UITextFieldDelegate>)textFieldsDelegate:(id<UITextViewDelegate>)textViewsDelegate;
  6. @parameters:

    • showDoneToolbar: If passed NO then Done Toolbar only will be added to the UITextViewtype fields. Otherwise on all fields.
    • textFieldsDelegate: Pass a common UITextFieldDelegate for all UITextFields in the form. You must not set delegates explicitly (such as textField.delegate=self) and must not make delegate connections through interface builder.
    • textFieldsDelegate: UITextViewDelegate

    This method is useful when you want to use delegates of the UITextFieldsand UITextViewDelegate.

  7. +(AutoScroller*) addAutoScrollTo:(UIScrollView*)scroll:(BOOL)showDoneButtonToolbar;

    This method is used when you want to show or hide done toolbar.

History

Your feedback is very important for us. I will try to implement your suggestions. Thanks.

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.

You may also be interested in...

Comments and Discussions

 
QuestionKeyboard Handling in iPhone Applications: Part 2/2 Pin
Member 1048211522-Dec-13 18:37
memberMember 1048211522-Dec-13 18:37 
QuestionRe: Keyboard Handling in iPhone Applications: Part 2/2 Pin
Member 1048211522-Dec-13 20:24
memberMember 1048211522-Dec-13 20:24 
AnswerRe: Keyboard Handling in iPhone Applications: Part 2/2 Pin
Sukhpal Singh JNU Jaipur12-Feb-14 0:13
memberSukhpal Singh JNU Jaipur12-Feb-14 0:13 
GeneralMy vote of 5 Pin
Avtar Guleria7-Mar-13 0:13
memberAvtar Guleria7-Mar-13 0:13 
Questionproblem with AutoScroller and more checks for textFields Pin
cruiserpaule13-Jan-13 19:29
membercruiserpaule13-Jan-13 19:29 
AnswerRe: problem with AutoScroller and more checks for textFields Pin
Sukhpal Singh JNU Jaipur13-Jan-13 21:03
memberSukhpal Singh JNU Jaipur13-Jan-13 21:03 
GeneralRe: problem with AutoScroller and more checks for textFields Pin
cruiserpaule13-Jan-13 21:54
membercruiserpaule13-Jan-13 21:54 
Here is the message

// ViewController.m
// LibTestUM

#import "ViewController.h"
#import "AutoScroller.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[AutoScroller addAutoScrollTo:self.scrollView];

+(AutoScroller*) addAutoScrollTo:(UIScrollView*)scroll:(BOOL)showDoneButtonToolbar:(id)textFieldsDelegate;
==> ! use of undeclared identifier "addAutoScrollTo: <==

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

// field-check
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string
{

// check of length
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (newLength > 5)
{
etc.
etc.

and why does the field-check not work ?
GeneralRe: problem with AutoScroller and more checks for textFields Pin
Sukhpal Singh JNU Jaipur13-Jan-13 22:18
memberSukhpal Singh JNU Jaipur13-Jan-13 22:18 
GeneralRe: problem with AutoScroller and more checks for textFields Pin
cruiserpaule18-Jan-13 18:36
membercruiserpaule18-Jan-13 18:36 
GeneralMy vote of 4 Pin
cruiserpaule13-Jan-13 10:11
membercruiserpaule13-Jan-13 10:11 
GeneralMy vote of 3 Pin
arif1462813-Jan-13 9:39
memberarif1462813-Jan-13 9:39 
GeneralMy vote of 5 Pin
Sukhpal Singh JNU Jaipur23-Dec-12 4:28
memberSukhpal Singh JNU Jaipur23-Dec-12 4:28 

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
Web01 | 2.8.150804.4 | Last Updated 14 Jan 2013
Article Copyright 2012 by Sukhpal Singh JNU Jaipur
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid