Add two textfields Example in Iphone


In this tutorial we will see how to enter the value to the textfield and on the button click we will get the addition on the label. For doing this we will take the values as a NSString and then we will type cast value in NSInteger and then perform addition again it will type cast in NSString to show on a label. Here are some steps for it…


1. Open the xcode & choose "File->New Project".
2. Select "Application" from left menu and then "View-based Application".
3.Name your project as "Add_two_labels" and save the project.
4.Now just select "Add_two_labelsViewController.h" file from left menu for define a UILabel and two UITextField . Set the property for UILabel , UITextField and define a method sum as IBAction.


#import

@interface Add_two_labelsViewController : UIViewController {
    IBOutlet UITextField *first_txt;
    IBOutlet UITextField *second_txt;
    IBOutlet UILabel *sum_label;
   

}
@property(nonatomic,retain)UITextField *first_txt;
@property(nonatomic,retain) UITextField *second_txt;
@property(nonatomic,retain)UILabel *sum_label;
-(IBAction)sum;

@end




5. Now select "Add_two_labelsViewController.m" file from menu for implementation.
#import "Add_two_labelsViewController.h"

@implementation Add_two_labelsViewController
@synthesize first_txt,second_txt,sum_label;
-(IBAction)sum
{

    NSString *t1=first_txt.text;
    NSInteger first=[t1 intValue];
    NSString *t2=second_txt.text;
    NSInteger second=[t2 intValue];
    NSInteger third=first+second;
    NSString *sum = [NSString stringWithFormat:@"%i",third];
    sum_label.text=sum;


}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [first_txt resignFirstResponder];
    [second_txt resignFirstResponder];
    [sum_label resignFirstResponder];
   
   
}


- (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 {
    [first_txt release];
    [second_txt release];
    [sum_label release];
    [super dealloc];
}

@end

6. Now double click on "Add_two_labelsViewController.xib" from Resource folder for open it.

7. On the view window drag the label,button and two textfield from library . Then link the label and textfields to the File's Owner. And link the button to IBActon method.

8. Then Quit and Save the Interface Builder.

9. Now you are ready for  click on "Build & Go" button.

Now you can see the output in iphone simulator .



(4) Responses to this post

Olivia May 05 2011 at 11:48:25

Your asnewr was just what I needed. ItÂ’s made my day!

hafrfz May 07 2011 at 05:34:11

sh1hjX , [url=http://epmibykosoor.com/]epmibykosoor[/url], [link=http://aczqrepepfyr.com/]aczqrepepfyr[/link], http://qysdeuokfcrq.com/

valium Mar 15 2012 at 00:16:17

http://www.shopdiazepamfast.com/ - valium without prescription buy valium cheap valium online buy generic valium http://www.shopdiazepamfast.com/

hqmsrbap Apr 15 2012 at 10:34:52

j1f41Q hjtwhfszdhwi, [url=http://zbfonwfolrlo.com/]zbfonwfolrlo[/url], [link=http://vtnldusxqetv.com/]vtnldusxqetv[/link], http://ogzzrucpbolc.com/


Leave your comment here






User Login




New User Forgot password

Newsletter Subscribe

Advertisement

 
 
 
quick contact