Click here to Skip to main content
Click here to Skip to main content
Alternative Tip
Go to top

Keyboard Handling in iPhone Applications: Part 2/2

, 14 Jan 2013
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 KeyboardHandler library. 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 UIView containing some TextFields and this UIView is 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 UIScrollView in it. Now add some UITextFields as shown in snapshot.
  8. The order of textfields must match with the order with textfields under scrollView in objects window as shown in the snapshot. I have given a number to each textfield to explain. Please see the snapshot.

  1. Open ViewController.xib and connect the UIScrollView to the IBoutlet scrollView created in the above step.
  2. Now download KeyboardHandler_Lib_v1.0.1___.zip and extract it.
  3. Add all three files from KeyboardHandler Lib 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 viewDidLoad method 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 UIScrollView containing form fields.
    • You should use this method in case you don't want to use delegate methods of UITextFields and 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 UITextView type 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 UITextView type 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 UITextFields and 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.

Comments and Discussions

 
QuestionKeyboard Handling in iPhone Applications: Part 2/2 PinmemberMember 1048211522-Dec-13 18:37 
QuestionRe: Keyboard Handling in iPhone Applications: Part 2/2 PinmemberMember 1048211522-Dec-13 20:24 
AnswerRe: Keyboard Handling in iPhone Applications: Part 2/2 PinmemberSukhpal Singh JNU Jaipur12-Feb-14 0:13 
GeneralMy vote of 5 PinmemberAvtar Guleria7-Mar-13 0:13 
Questionproblem with AutoScroller and more checks for textFields Pinmembercruiserpaule13-Jan-13 19:29 
Hi,
 
I use so far only in ViewController.m this line => [AutoScroller addAutoScrollTo:scrollView];
The app work and the view scrolled up when I go to the last textField.
 
But I have also for each textField a delegate to check all fields with "field length = 5 and inputs like 0, 1, ..9" and other checks.
 
check begins with ...
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (newLength > 5)
etc.
etc.
 
These checks doesnt work when I use the AutoScroller.
 
You give us some informations about "Other important methods of AutoScroller class".
Is this the solution for my problem?
 
Pls can you explain and give me some examples how can I write in my code the right lines of the other methods of AutoScroller.
I got errors when I write ... +(AutoScroller*) addAutoScrollTo:(UIScrollView*)scroll:(BOOL)showDoneButtonToolbar:(id)textFieldsDelegate;
 
Thnks in advance.
AnswerRe: problem with AutoScroller and more checks for textFields PinmemberSukhpal Singh JNU Jaipur13-Jan-13 21:03 
GeneralRe: problem with AutoScroller and more checks for textFields Pinmembercruiserpaule13-Jan-13 21:54 
GeneralRe: problem with AutoScroller and more checks for textFields PinmemberSukhpal Singh JNU Jaipur13-Jan-13 22:18 
GeneralRe: problem with AutoScroller and more checks for textFields Pinmembercruiserpaule18-Jan-13 18:36 
GeneralMy vote of 4 Pinmembercruiserpaule13-Jan-13 10:11 
GeneralMy vote of 3 Pinmemberarif1462813-Jan-13 9:39 
GeneralMy vote of 5 PinmemberSukhpal 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 | Mobile
Web02 | 2.8.140905.1 | Last Updated 14 Jan 2013
Article Copyright 2012 by Sukhpal Singh JNU Jaipur
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid