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 Reply

Your email address will not be published. Required fields are marked *