iPhone Input Field, iPhone Input Field Tutorial


 

iPhone Input Field, iPhone Input Field Tutorial

Generally, input fields are used to take input from the users. In iPhone application we can use Text Field to take the input.

Generally, input fields are used to take input from the users. In iPhone application we can use Text Field to take the input.

iPhone Input Field

Generally, input fields are used to take input from the users.  In iPhone application we can use Text Field to take the input.  Text field displays editable text and once you taps it'll display a keyboard. That keyboard can be customize according to our need i.e. if we wants the user to enter only numbers then we can choose a number pad.   

In this example we are going to create a small application that will take the input in three different forms..

1. URL
2. Password &
3. Number
This is a very simple "iPhone input field" example that will show you how to use input field according to your requirement.

Creating an Application..
1. Create a new view based project and name it as "input-field".
2. Open input_fieldViewController.xib file that resides in resources folder
3. And now place labels and Text Field according to your requirement. I have taken four labels and three text boxes. Save and close the .xib file.


4. Now open your input_fieldViewController.h file and define the text field and labels,  create property and synthesize it into the input_fieldViewController.m file. see the given code for details..

 Code for input_fieldViewController.h

#import <UIKit/UIKit.h>

 @interface input_fieldViewController : UIViewController {

    IBOutlet UITextField *URL;

    IBOutlet UITextField *Password;

    IBOutlet UITextField *Number;

}

 @property (nonatomic retain) IBOutlet UITextField *URL;

@property (nonatomic retain) IBOutlet UITextField *Password;

@property (nonatomic retain) IBOutlet UITextField *Number;

 @end

Code for input_fieldViewController.m

#import "input_fieldViewController.h"

 @implementation input_fieldViewController

@synthesize URL;

@synthesize Password;

@synthesize Number;

 - (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

     // Release any cached data, images, etc that aren't in use.

}

 -(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event

{

    [URL resignFirstResponder];

    [Password resignFirstResponder];

    [Number resignFirstResponder];

    [super touchesBegan:touches withEvent:event ];

}

 - (void)viewDidUnload {

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

 - (void)dealloc {

    [super dealloc];

}

 @end

 

In the above code you can see that we have also written a void method the disappear the keyboard on clicking outside the text field.

 Here is the code to hide the keyboard in iphone application

-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event

{

    [URL resignFirstResponder];

    [Password resignFirstResponder];

    [Number resignFirstResponder];

    [super touchesBegan:touches withEvent:event ];

}

Now, it's time to make a connection between text field with the text field name. For that open input_fieldViewController.xib file once again and..

1. Click on the File Owner
2. Link the Text Field to their outlets ...just like given in the image below.


3. Save and close the .xib file.

Now, click on build & go button to  run your application. Once you taps in the text field you'll find that all the text fields are showing the similar type of keypads by default. 

To customize that text field in iPhone open the .xib file and then open "inspector" from tool bar. Select the text field and change the keyboard type. 

For the Password you need to check the Secure box that's it. And it'll create your application with  three types of iPhone text field.

 

Download Code

Ads