Home Tutorial Iphone Examples iphone text field example

 
 

Share on Google+Share on Google+
iphone text field example
Posted on: September 1, 2009 at 12:00 AM
Advertisement
In this iPhone example you are going to see...how to add the value of two different textfield and printing it in another textfield. This is a quite easy! What all we have to do is to take three different textfield and assign them different names. We have named it textField, textField1 & textField2.

iphone textfield with example

 

In this iphone example you are going to see...how to add the value of two different textfield and printing it in another textfield. This is a quite easy! What all we have to do is to take three different textfield and assign them different names. We have named it textField, textField1 & textField2.

Now lets find out what is happening in the application...

As i said above there are three different textFields used in this application, First 2 takes the input from the user and the last text field prints the added values of above text fields into it on button click(Submit button). The application will look like the given image...

 

The last textfield is not editable that means... a user can't edit or enter value in it. It's used to print the returned value.

-1.         

 Note: To make a textfield not editable...select the open the project .XIB file and select the desired textfield that you wanted to make only readable. Now go to Tools->Inspector->Text Field Attributes-> Interaction and deselect the check box.

Now let's check the code:

As you know, this is a view based application. So there are 2 different files appDelegate(.h & .m) and ViewController(.h, .m). We 'll be writing our code in ViewController file.

In this we are writing action on the submit button used to show two string at one place or on single TextField.NSString is used to enter the string into the textfield and to display.

AddTwoTextFieldViewController.h

#import <UIKit/UIKit.h>

@interface AddTwoTextFieldViewController : UIViewController

{

    IBOutlet UITextField *textField;

    IBOutlet UITextField *textField1;

    IBOutlet UITextField *textField2;   

    NSString *String;

  }

 @property (nonatomic retain) IBOutlet UITextField *textField;

@property (nonatomic retain) IBOutlet UITextField *textField1;

@property (nonatomic retain) IBOutlet UITextField *textField2;

@property (nonatomic, copy) IBOutlet NSString *String;

 - (IBAction)changrGreeting ;

 @end

 The @synthesize keyword tells the compiler to generate getter/setter functions for the named variables and in action we are writing the two string into different field and showing it on single field, this is done by

-(IBAction)changrGreeting ;    

{

    self.String = textField.text;

    NSString *nameString = String;

    self.String = textField1.text;

    NSString *nameString1 = String;  

   if ([nameString length] == 0)

    {

        nameString =@"   ";

    }

    if ([nameString1 length] == 0) 

    {

        nameString1 =@"    ";

    } 

 /* to display am using another field named as textField2 and is displayed by using, space is used to show two string separately first is first textfield and second is another.*/

 

  textField2.text=[NSString stringWithFormat:@"%@  %@", textField.text, textField1.text]; 

 }

AddTwoTextFieldViewController.m

#import "AddTwoTextFieldViewController.h"

#import "AddTwoTextFieldAppDelegate.h"

@implementation AddTwoTextFieldViewController

@synthesize textField;

@synthesize textField1;

 @synthesize textField2;

@synthesize String;

-(IBAction)changrGreeting ;    

 {

    self.String = textField.text;

    NSString *nameString = String;

    self.String = textField1.text;

    NSString *nameString1 = String;  

    if ([nameString length] == 0)

    {

        nameString =@"   ";

    }

    if ([nameString1 length] == 0) 

    {

        nameString1 =@"    ";

    } 

textField2.text=[NSString stringWithFormat:@"%@  %@", textField.text, textField1.text];

 }

    // this method is used to resign the keyboard 

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

{

    [textField resignFirstResponder];

    [textField1 resignFirstResponder];

    [super touchesBegan:touches withEvent:event ];

}

- (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)viewDidUnload {

    // Release any retained subviews of the main view.

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

}

- (void)dealloc {

    [textField release];

    [textField1 release];

    [textField2 release];

    [String release];

    [super dealloc];

}

@end

Now open the interface builder and associate each textfield outlets in file's owner. As given in the image...

That's it! Now save and run the application by clicking on Build & Go button in Xcode.

Click to Download Code

 

Advertisement

Related Tags for iphone text field example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 1, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com