Saturday, April 14, 2012

Simple Addition

In this post we will see how to create a small application to add two numbers. 

Our final view is this one for which we will be coding,




Step 1: Create a window based application and add a view controller class file with an appropriate name (click here  if you are facing issues).

Step 2: As per our view we will require two objects of UITextField and UILabel and one object of button class, so in the .h file declare those objects. Heres a view of my .h file





Step 3: Now into the .m file we will initialize the declared objects into the init method, heres how you do that

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
//setting the textfield...
firstnumber = [[UITextField alloc]initWithFrame:CGRectMake(1241239731)];
[firstnumber setBorderStyle:UITextBorderStyleRoundedRect];
secondnumber = [[UITextField alloc]initWithFrame:CGRectMake(1241629731)];
[secondnumber setBorderStyle:UITextBorderStyleRoundedRect];
//setting the button...
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(1362097237)];
[btn setTitle:@"Add" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(AdditionforControlEvents:UIControlEventTouchUpInside];
//setting the label...
lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(201235821)];
lbl1.text = @"First no";
lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(201628221)];
lbl2.text = @"Second no";
    }
    return self;
}

Step 4: Giving body to the Addition function

-(void)Addition
{
int x= [firstnumber.text intValue];                   //typecasting the string into integer
int y = [secondnumber.text intValue];           //typecasting the string into integer
int result = x+y;                                                  //adding the two number
NSString *str = [NSString stringWithFormat:@"%d",result];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Simple Addition" message:str delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
[alert show];                                                     //display the alert view.
[alert release];
}

Step 5: Loading the views to the self view with the help of the loadView method

- (void)loadView {
[super loadView];
[self.view addSubview:firstnumber];
[self.view addSubview:secondnumber];
[self.view addSubview:btn];
[self.view addSubview:lbl1];
[self.view addSubview:lbl2];
}



Step 6: Finally go to the app delegate .m file and add the view to the window of the iPhone, heres how you do it. 
The name of my app delegate file is SimpleAddition, so heres how you do it

#import "SimpelAdditionAppDelegate.h"
#import "FIrstviewcontroller.h" 

@implementation SimpelAdditionAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
  // Override point for customization after application launch
FIrstviewcontroller *objfirstview = [[FIrstviewcontroller alloc]init];
[window addSubview:objfirstview.view];
 [window makeKeyAndVisible];
}

Step 7: now when you run the application your iPhone simulator will have the following look.



And when you enter any number, and hit the add button you will see your result in an alert view.

Final output

This was my post on simple addition in an iPhone, Happy iCoding.

No comments:

Post a Comment