iphone text field example


 

iphone text field 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.

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;

0

    NSString *nameString = String;

    self.String = textField1.text;

    NSString *nameString1 = String;  

1

   if ([nameString length] == 0)

    {

        nameString =@"   ";

2

    }

    if ([nameString1 length] == 0) 

    {

3

        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.*/

4

 

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

 }

5

AddTwoTextFieldViewController.m

#import "AddTwoTextFieldViewController.h"

#import "AddTwoTextFieldAppDelegate.h"

6

@implementation AddTwoTextFieldViewController

@synthesize textField;

@synthesize textField1;

7

 @synthesize textField2;

@synthesize String;

-(IBAction)changrGreeting ;    

8

 {

    self.String = textField.text;

    NSString *nameString = String;

9

    self.String = textField1.text;

    NSString *nameString1 = String;  

    if ([nameString length] == 0)

0

    {

        nameString =@"   ";

    }

1

    if ([nameString1 length] == 0) 

    {

        nameString1 =@"    ";

2

    } 

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

 }

3

    // this method is used to resign the keyboard 

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

{

4

    [textField resignFirstResponder];

    [textField1 resignFirstResponder];

    [super touchesBegan:touches withEvent:event ];

5

}

- (void)didReceiveMemoryWarning {

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

6

    [super didReceiveMemoryWarning];

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

}

7

- (void)viewDidUnload {

    // Release any retained subviews of the main view.

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

8

}

- (void)dealloc {

    [textField release];

9

    [textField1 release];

    [textField2 release];

    [String release];

0

    [super dealloc];

}

@end

1

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.

2

Click to Download Code

 

Ads