Combining Two Textfield Values And Displaying It In Another Textfield

This is the “AddTwoTextField” example. I am going to show you the how to add two text field values. In this app you can add the value of two different textfields and print it in another textfield.

Step 1: Open the Xcode and create a new Xcode project using View base application template. Give the application name “AddTwoTextField”. As shown in the figure below:

Step 2: Expand classes and notice Interface Builder created the AddTwoTextFieldViewController.h and AddTwoTextFieldViewController.m class for you. Expand Resources and notice the template generated a separate nib, TwotextFieldViewController.xib.

Step 3: Open the AddTwoTextFieldViewController.h file and we have to add

IBOutlet UITextField *textdata;
IBOutlet UITextField *textdata1;
IBOutlet UITextField *textdata2;
IBOutlet UITextField *textdata;

 

For displaying the textfields mention IBAction and to perform the given action make the following changes in the file.

#import <uikit/UIKit.h>

@interface AddTwoTextFieldViewController : UIViewController
{
IBOutlet UITextField *textdata;
IBOutlet UITextField *textdata1;
IBOutlet UITextField *textdata2;
NSString *String;
}

@property (nonatomic retain) IBOutlet UITextField *textdata;
@property (nonatomic retain) IBOutlet UITextField *textdata1;
@property (nonatomic retain) IBOutlet UITextField *textdata2;
@property (nonatomic, copy) IBOutlet NSString *String;
- (IBAction)SubmitB;
@end

 

 

Step 4: Double click the AddTwoTextFieldViewController.xib file and after that make the following changes.
A) Open the view window, first drag the Round Rect Button from the library and place it to the view window and select the button.

B) Open the view window, and drag the TextField from the library and place it to the view window.

Step 5: Open the AddTwoTextFieldViewController.m file and make the following changes in the file.

#import "AddTwoTextFieldViewController.h"
#import "AddTwoTextFieldAppDelegate.h"

@implementation AddTwoTextFieldViewController

@synthesize textdata;
@synthesize textdata1;
@synthesize textdata2;
@synthesize String;

-(IBAction)SubmitB;

{
self.String = textdata.text;
NSString *nameString = String;
self.String = textdata1.text;
NSString *nameString1 = String;

if ([nameString length] == 0)
{
nameString =@" ";
}
if ([nameString1 length] == 0)
{
nameString1 =@" ";
}

textdata2.text=[NSString stringWithFormat:@"%@ %@", textdata.text, textdata1.text];

}

-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event
{
[textdata resignFirstResponder];
[textdata1 resignFirstResponder];
//[textField2 resignFirstResponder];
[super touchesBegan:touches withEvent:event ];
}

 

 

Step 6: Now build and run the code and view the Output in the Simulator.

You can download source code from here AddTwoTextField

Leave a Comment:

3 comments
Patrick says April 11, 2011

Thanks! very helpful.

Reply
Edwin Sandoval says April 13, 2011

Excelente article man, maybe you should to add more comments in order to do easier to read it.

Reply
Jeremy Hilton says May 7, 2011

Thank you for the tutorial. I agree comments would help.

Additionally, you have some unnecessary code that might be confusing users. The whole block of code below can be removed without issue.

self.String = textdata.text;
NSString *nameString = String;
self.String = textdata1.text;
NSString *nameString1 = String;

if ([nameString length] == 0)
{
nameString =@” “;
}
if ([nameString1 length] == 0)
{
nameString1 =@” “;
}

This is completely unneeded as you use the values of textdata.text and textdata1.text for building your string later on in the routine, not nameString and nameString1.

textdata2.text=[NSString stringWithFormat:@”%@ %@”, textdata.text, textdata1.text];

Thanks again.

Reply
Add Your Reply