PDA

View Full Version : Returning a sum with Objective-C




haiggy
Mar 26, 2009, 10:10 PM
Hey,
I'm relatively new at programming and am able to do something as trivial as returning a sum of two variables etc in C, but I'm trying to discover how I would do this using Objective-C.

I am programming this for the iPhone.

What I want to do is have two labels where once a single button is pressed, the two values from two textfields are transferred to two labels. I can do this with ease. The trouble I'm having comes from having another button calculate the sum of the values in the two labels. How would I go about doing this? I don't have any example code but if you would like I can add some later. I'm just looking for a rough idea of how to grab the strings and calculate the sum. I may be overlooking something but I can't ever get it to work.

Any help would be appreciated :)
Thanks!



lancestraz
Mar 26, 2009, 10:21 PM
I think you would have to use something like: int number = [textField intValue]; on the text fields and then add em up.

lee1210
Mar 26, 2009, 10:29 PM
NSTextField is an NSControl. It responds to intValue, which returns an int. It also responds to setIntValue:, which accepts an int. So you could do:
[resField setIntValue: [val1Field intValue] + [val2Field intValue]];

-Lee

haiggy
Mar 26, 2009, 10:30 PM
NSTextField is an NSControl. It responds to intValue, which returns an int. It also responds to setIntValue:, which accepts an int. So you could do:
[resField setIntValue: [val1Field intValue] + [val2Field intValue]];

-Lee

Thanks so much, I will try this out. Makes sense now. Thanks for the fast reply guys! :)

haiggy
Mar 27, 2009, 09:39 AM
Hmmm... I've tried tinkering around with your different suggestions but nothing works. It says setIntValue etc may not be recognized by UITextField... weird. Can someone help me out and write the code for the sumOfValues button?

#import "SampleViewController.h"

@implementation SampleViewController
@synthesize number1,number2,textNumber1,textNumber2,addedValues;

- (IBAction)changeValues:(id)sender;
{
NSString *change = [textNumber1 text];
NSString *newChange = [[NSString alloc] initWithFormat:@"%@", change];
number1.text = newChange;
[newChange release];

NSString *change2 = [textNumber2 text];
NSString *newChange2 = [[NSString alloc] initWithFormat:@"%@", change2];
number2.text = newChange2;
[newChange2 release];

}

- (IBAction)sumOfValues:(id)sender;
{

}

Thanks

lee1210
Mar 27, 2009, 10:08 AM
when you said textfield it was a bit ambiguous. You are dealing with a UITextField and not an NSTextField. With UITextField you'll have to do a little more work, because it looks like you can just get the value passing the message text. I don't see anything to get the value interpreted as other types... so:


UITextField resField = ...;
UITextField val1Field = ...;
UITextField val2Field = ...;
...
[resField setText: [NSString stringWithFormat:@"%d",[[val1Field text] intValue] + [[val2Field text] intValue]]];


The property text of UITextField is an NSString. You can pass the message intValue to an NSString to get the current value interpreted as an integer. Since int is a primitive, there's no methods like stringValue, etc. to use, so i just used NSString's stringWithFormat, but there may be a quicker way to handle this. If you wanted this broken down/more verbose, it's the same as something like this:

int x = [[val1Field text] intValue];
int y = [[val2Field text] intValue];
int sum = x + y;
NSString *sumString = [NSString stringWithFormat:@"%d",sum];
[resField setText:sumString];


-Lee