Returning a sum with Objective-C

Discussion in 'Mac Programming' started by haiggy, Mar 26, 2009.

  1. macrumors 65816

    haiggy

    Joined:
    Aug 20, 2003
    Location:
    Ontario, Canada
    #1
    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!
     
  2. macrumors 6502a

    lancestraz

    Joined:
    Nov 27, 2005
    Location:
    RI
    #2
    I think you would have to use something like: int number = [textField intValue]; on the text fields and then add em up.
     
  3. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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
     
  4. thread starter macrumors 65816

    haiggy

    Joined:
    Aug 20, 2003
    Location:
    Ontario, Canada
    #4
    Thanks so much, I will try this out. Makes sense now. Thanks for the fast reply guys! :)
     
  5. thread starter macrumors 65816

    haiggy

    Joined:
    Aug 20, 2003
    Location:
    Ontario, Canada
    #5
    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
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     

Share This Page