Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

haiggy

macrumors 65816
Original poster
Aug 20, 2003
1,328
76
Ontario, Canada
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

macrumors 6502a
Nov 27, 2005
898
0
RI
I think you would have to use something like: int number = [textField intValue]; on the text fields and then add em up.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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:
Code:
[resField setIntValue: [val1Field intValue] + [val2Field intValue]];

-Lee
 

haiggy

macrumors 65816
Original poster
Aug 20, 2003
1,328
76
Ontario, Canada
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:
Code:
[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

macrumors 65816
Original poster
Aug 20, 2003
1,328
76
Ontario, Canada
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?

Code:
#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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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:

Code:
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:
Code:
int x = [[val1Field text] intValue];
int y = [[val2Field text] intValue];
int sum = x + y;
NSString *sumString = [NSString stringWithFormat:@"%d",sum];
[resField setText:sumString];

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.